How is it possible to have an instance of a class which is an object
, without the class being a subclass of object
? here is an example:
>>> class OldStyle(): pass
>>> issubclass(OldStyle, object)
False
>>> old_style = OldStyle()
>>> isinstance(old_style, object)
True
In Python 2, type and class are not the same thing, specifically, for old-style classes,
type(obj)
is not the same object asobj.__class__
. So it is possible because instances of old-style classes are actually of a different type (instance
) than their class:This is resolved in new-style classes:
Old-style class is not a type, new-style class is:
Old style classes are declared deprecated. In Python 3, there are only new-style classes;
class A()
is equivalent toclass A(object)
and your code will yieldTrue
in both checks.Take a look at this question for some more discussion: What is the difference between old style and new style classes in Python?
Everything is an object:
Note that this question has essentially nothing to do with old- vs. new-style classes.
isinstance(old_style, object)
beingTrue
is simply a corollary of the fact that every value in python is an instance ofobject
.When you do the expression
It means you are instantiating the object, which old_style is an instance of the class OldStyle.
Also, both evaluates to True in Python 3.2.