__bases__ misleading error message

Terry Reedy tjreedy at udel.edu
Sat Jan 24 14:24:41 EST 2015


On 1/24/2015 5:16 AM, Mario Figueiredo wrote:
>
> Consider the following code at your REPL of choice

>         class Sub:
>             pass
>
>         foo = Sub()
>
>         Sub.__bases__
>         foo.__bases__
>
> The last statement originates the following error:

This is an anomalous situation.  Normally, if a class has an attribute, 
instances have the same attribute (unless overriden).  But this does not 
matter.

>         AttributeError: 'Sub' object has no attribute '__bases__'

In this message, 'Sub' is an adjective, modifying 'object, not naming 
it.  If you attend to the last line of the traceback

     foo.__bases__

which is an integral part of the total error message, the interpretation 
of this exception message should be clear.

>         AttributeError: 'Sub' instance has no attribute '__bases__',

'Sub' is an instance of 'type', so "'Sub' instance" could also be taken 
to mean the class if the traceback were ignored.  But that would also be 
wrong.  An attribute error for the class, such as "Sub.xyz" give a 
message ending with
...
     Sub.xyz
AttributeError: type object 'Sub' has no attribute 'xyz'

-- 
Terry Jan Reedy




More information about the Python-list mailing list