Comparing two class types in python

Explanation

This is why your comparison doesn't work as expected

>>> class ClassA(object):
...     pass
... 
>>> class ClassB(object):
...     pass
... 
>>> type(ClassB)
<class 'type'> 
>>> type(ClassA)
<class 'type'> 
>>> type(ClassA) == type(ClassB)
True

But why do ClassA and ClassB have the same type type? Quoting the docs:

By default, classes are constructed using type(). The class body is executed in a new namespace and the class name is bound locally to the result of type(name, bases, namespace).

Example:

>>> ClassB
<class '__main__.ClassB'>
>>> type('ClassB', (), {})
<class '__main__.ClassB'>
>>> type(ClassB)
<class 'type'>
>>> type(type('ClassB', (), {}))
<class 'type'>

Getting the type of ClassB is exactly the same as getting the type of type('ClassB', (), {}), which is type.

Solutions

Compare them directly (w/out using the type() function):

>>> ClassA
<class '__main__.ClassA'>
>>> ClassB
<class '__main__.ClassB'>
>>> ClassA == ClassB
False

or initialize them and compare the types of their objects:

>>> a = ClassA()
>>> b = ClassB()
>>> type(a) 
<class '__main__.ClassA'>
>>> type(b) 
<class '__main__.ClassB'>
>>> type(a) == type(b)
False

FWIW you can also use is in place of == (for classes).


If you want to check if types are equal then you should use is operator.

Example: we can create next stupid metaclass

class StupidMetaClass(type):
    def __eq__(self, other):
        return False

and then class based on it:

  • in Python 2

    class StupidClass(object):
        __metaclass__ = StupidMetaClass
    
  • in Python 3

    class StupidClass(metaclass=StupidMetaClass):
        pass
    

then a simple check

StupidClass == StupidClass

returns False, while the next check returns an expected True value

StupidClass is StupidClass

So as we can see == operator can be overridden while there is no simple way to change is operator's behavior.


You're comparing the type of the class object, which are all of type 'type'.

If you just want to compare the classes, compare them directly:

print Class3 == Class4

Tags:

Python

Types