When is type(instance) different from instance.__class__?
type(instance)
and instance.__class__
may be different, even with new-style classes, as Guido mentioned in PEP 3119:
Also,
isinstance(x, B)
is equivalent toissubclass(x.__class__, B) or issubclass(type(x), B)
. (It is possibletype(x)
andx.__class__
are not the same object, e.g. whenx
is a proxy object.)
For instance:
class A:
pass
class B:
__class__ = A
b = B()
print(type(b)) # prints <class '__main__.B'>
print(b.__class__) # prints <class '__main__.A'>