Can anyone explain this behaviour (class attribute vs instance lookup)

Bruno da Silva de Oliveira nicoddemus at gmail.com
Tue Mar 7 07:16:46 EST 2006


Hi all,

>From what I understand about the lookup semantics of instances and classes,
the following is expected:

>>> class A(object): pass
...
>>> A.__module__
'__main__'
>>> A().__module__
'__main__'

IOW, if an attribute is not found in the instance, it is looked-up in class,
and if found, returned. Then, I was pluzzed by this behaviour:

>>> A.__name__
'A'
>>> A().__name__
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'A' object has no attribute '__name__'

It seems that '__name__' is being treated differently somehow. I guess this
involves the descriptor machinery, but I don't know the internals of the
new-class completely to explain the above behaviour in detail. Can anyone
explain precisely the above semantics?

Best Regards,
Nicodemus.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060307/cb6e23b1/attachment.html>


More information about the Python-list mailing list