4

This is similar to another question but in this case I would like to understand the type comparison between two class types that are the same but are dynamically created.

Consider an example from this SO question:

class SecretBaseClass(object):
    pass

class Class(object):
    pass

ClassType1 = type(Class.__name__, (SecretBaseClass,), dict(Class.__dict__))
ClassType2 = type(Class.__name__, (SecretBaseClass,), dict(Class.__dict__))

If I then do:

print ClassType1 == ClassType2

my result is false.

I get that I've created two distinct types, but to a human they are the same. At what level does the comparison operator recognise the difference?

Community
  • 1
  • 1
E.Beach
  • 1,829
  • 2
  • 22
  • 34

1 Answers1

6

If you don't define the comparison magic method __eq__, the default behaviour for a == b is id(a) == id(b), i.e. are they the same object. In this case, they aren't; they are two separate type objects.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437