__bases__ misleading error message

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jan 24 13:43:17 CET 2015

Mario Figueiredo wrote:

> Consider the following code at your REPL of choice
>         class Super:
>             pass

Super is irrelevant here, since it isn't used.

>         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. This is, in my opinion, a Java-ism which is
inappropriate for Python where classes are objects too, but we seem to be
stuck with it.

So we have a Sub instance (object) which has no attribute '__bases__'. This
is no different from:

py> (23).spam
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'spam'

> 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?

> It's the instance 
> that has not. So shouldn't the error read like:
>         AttributeError: 'Sub' instance has no attribute '__bases__', or
>         AttributeError: 'foo' object has no attribute '__bases__'

The first would be nice. The second is impossible: objects may have no name,
one name, or many names, and they do not know what names they are bound to.
So the Sub instance bound to the name 'foo' doesn't know that its name
is 'foo', so it cannot display it in the error message.


More information about the Python-list mailing list