__bases__ misleading error message

Marco Buttu marco.buttu at gmail.com
Sat Jan 24 23:09:31 CET 2015


On 24/01/2015 13:43, Steven D'Aprano wrote:

> Mario Figueiredo wrote:
>
>> >         class Sub:
>> >             pass
>> >
>> >         foo = Sub()
>> >
>> >         Sub.__bases__
>> >         foo.__bases__
>> >
>> >The last statement originates the following error:
>> >
>> >         AttributeError: 'Sub' object has no attribute '__bases__'
> It's a bit ambiguous, but the way to read it is to think of object as a
> synonym for instance.
...
>
>> >Naturally the 'Sub' object has an attribute __bases__.
> Correct, in the sense that classes are objects too. But in the sense of
> object=instance, no. Isn't ambiguous terminology wonderful?

In my opinion it is not ambiguous, because as you say in Python an 
object is a synonym for instance. We can say in Python an object is an 
instance of the type object, that's all. And that's why everything is an 
object in Python:

 >>> isisntace(foo, object) # For every foo
True

So a class (a type) is an instance too, an instance of its metaclass. 
Therefore, the message says that the Sub instance has no attribute 
'__bases__'. In fact the Sub object (instance) has no attribute 
'__bases__' because it is not an instance of type, and only types 
(classes) have this attribute.

So if we want to find some differences between objects, we have to 
distinguish between types (classes) and non-types (non-classes) objects, 
and not between objects and classes.
In the Python documentation there is also a bit of confusion about 
this... In the descriptor howto: "The details of invocation depend on 
whether obj is an object or a class" :/


-- 
Marco Buttu



More information about the Python-list mailing list