An object is an instance (or not)?
steve+comp.lang.python at pearwood.info
Wed Jan 28 09:45:57 CET 2015
random832 at fastmail.us wrote:
> On Tue, Jan 27, 2015, at 16:06, Mario Figueiredo wrote:
>> That error message has me start that thread arguing that the error is
>> misleading because the Sub object does have the __bases__ attribute.
>> It's the Sub instance object that does not have it.
> What do you think "Sub object" means?
I think it means the Sub class, which is an object. And Python 3.3 agrees
py> class Sub(object):
py> isinstance(Sub, object)
Sub is also an instance of type. And object is an instance of type, type is
an instance of object, and type is a subclass of object. But object is not a
subclass of type. However it is an instance of type. Are we confused yet?
There is a sense, taken from Java in particular, that "object" is synonymous
for "instance". For example, Oracle's documentation for classes says:
"A class contains constructors that are invoked to create objects from the
But you won't find any Java documentation referring to the class itself as
an object. Java has no metaclasses, classes are not instances of a
metaclass, and so classes are never instances. Classes are classes, objects
are instances of a class, and that is all there is to it.
But in Python, "object" and "instance" are more ambiguous, thanks to
metaclasses. Instances of a metaclass are classes, hence classes are
instances as well as classes:
py> isinstance(Sub, type)
Sub is an instance of the class "type". And because classes themselves are
first-class (ha ha!) values, like ints, strings, lists etc., classes are
All classes (in Python 3) are instances of a class (the metaclass), but not
all instances are classes.
Obviously this is rather confusing, but fortunately metaclasses (other than
type itself) are rare, so *most of the time* we can get away with Java-like
terminology, and pretend that classes are distinct from instances rather
than being just another kind of instance (of the metaclass).
But what we cannot do is pretend that "object" means instance, because all
classes are objects. Alas, habits from other languages leak in, and the
Javaesque terminology "Foo object" meaning an instance of Foo sometimes gets
used, even though "Foo object" should refer to the object called Foo, namely
the class object itself.
The unfortunate ambiguity is that sometimes "Foo object" gets used to mean
Foo, and other times it gets used to me some instance of Foo. We say:
"the None object has no state"
"the function requires an int object as argument".
> Sub itself is not a Sub object,
Sub itself is not *a* Sub object, it is *the* Sub object, just as 42 is
*the* object with type int and value forty-two.
> it is a type object. "instance" is
> implicit in the phrase "foo object".
Alas, classes *are* instances, of their metaclass, so "Sub object" could
refer to an instance of Sub (using the Javaesque terminology) or Sub itself
(using more Pythonistic terminology).
More information about the Python-list