__bases__ misleading error message

Marco Buttu marco.buttu at gmail.com
Sat Jan 24 09:12:36 EST 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.

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 (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.

That means in Python sometimes, like the OP one, we have to underline 
the difference between types (classes) and non-types (non-classes) 
objects, instead of between objects and classes. The problem is 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

INAF-Osservatorio Astronomico di Cagliari
Via della Scienza n. 5, 09047 Selargius (CA)
Phone: 070 711 80 217
Email: mbuttu at oa-cagliari.inaf.it




More information about the Python-list mailing list