Explain Codes LogoExplain Codes Logo

How to check whether a variable is a class or not?

python
type-checking
reflection
custom-inference
Nikita BarsukovbyNikita Barsukov·Feb 15, 2025
TLDR

The answer to "how to identify if a variable is a class" lies in using inspect.isclass(variable), provided by Python's inspect module.

import inspect # Think of 'obj' as a chameleon, changing colors (or in this case types)! print("Is a class:", inspect.isclass(obj))

It will return True if the variable is a class, False otherwise.

Step-by-step guide through Python's type checking

Let's guide through the Python trail to understand the wild west of checking whether variables are classes or not.

The Type Sheriff: type()

The simple notion behind the type() function is that for any class fed into it, it should return <class 'type'>. This includes user-defined classes!

class MyClass: pass # Who are you, MyClass? A class, perhaps? print(type(MyClass)) # Output: <class 'type'>

Identity Theft Check: isinstance()

Classes impersonating other types? No more with isinstance(). Classes are essentially instances of the type type (unsurprisingly).

# MyClass, are you sure you are a real class? No identity theft? print(isinstance(MyClass, type)) # Output: True

Python 2's Old West: types.ClassType

In the case of Python 2, a rogue called types.ClassType represents old-style classes. Capture both new and old-style classes with (type, types.ClassType).

import types # OldClass doing a vintage comeback in Python 2? Let's inspect! print(isinstance(OldClass, (type, types.ClassType)))

Spotting a class in a crowd

Learn to decipher whether a variable is a class based on various recognizable traits and missteps.

Indicators to tick off

Certain attributes unique to classes like __doc__ and __module__ usually don't parade around in other variables:

# Knock Knock! Does your mystery variable have a __doc__ attribute? print(hasattr(MyClass, '__doc__')) # Often True for classes

Avoid the Python Police: 'class' keyword

Keywords like class are reserved. Stay on the right side of the law! Use type for class variable checks, but remember to keep your variables clearly named.

# Rename your variables. Don't be a Billy-the-Kid-class-outlaw! class_type = MyClass # Type names are badges. Wear them with honour!

Crossing the Python 2 and 3 divide: six.class_types

The six library is a peace treaty between Python 2 and 3, with its six.class_types offering:

import six # MyClass doing a 'Python version' rope trick? print(isinstance(MyClass, six.class_types)) # True for class objects in Python 2 and 3

Mastering the art of class detection

Let's deep dive into Python's mystical world of 'reflection' and 'custom inference' while class-detecting.

Metaclass Mystery • Reflection

Python's magic trick of reflection lets you peek and prod at your classes. Metaphorically check the DNA of your class!

is_metaclass = isinstance(type(MyClass), type) # Who are you really, MyClass, under that 'class' disguise? A metaclass? print("Is a metaclass:", is_metaclass)

Custom Type Checks

Craft your custom detection function to branch out from usual indicators - beyond the classes, into the unknown.

def is_custom_class(obj): return hasattr(obj, '__dict__') and not callable(obj) and hasattr(obj, '__module__') # Fancy dinner check-in - Ensure MyClass brings __dict__, isn't callable, and has __module__ print(is_custom_class(MyClass))

Remember to deep dive only when you're confident swimming in Python's class pool.