[issue14092] __name__ inconsistently applied in class definition

Terry J. Reedy report at bugs.python.org
Sat Feb 25 02:15:32 CET 2012


Terry J. Reedy <tjreedy at udel.edu> added the comment:

This is not a bug report, as Python works as documented. 
Double underscore names are defined as *reserved* for the interpreter, with the ones actually in use having defined meanings.

type.__new__ sets several internally used attributes on new classes. The attribute look-up mechanism for classes looks at them first before looking in __dict__, which is for attributes of both the class and its instances. Here is another example similar to yours.

>>> class C: __dict__ = 1

>>> C.__dict__
dict_proxy({'__dict__': 1, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'C' objects>, '__doc__': None})
>>> C().__dict__
1

__dict__ is not writable, but __class__ is. You can already rename a class if you really want:

>>> C.__name__ = 'bizarre'
>>> C.__name__
'bizarre'

Conceptually, this seems the right way as one normally would not want the name of the class to be the default name for every instance.

>>> C().__name__
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    C().__name__
AttributeError: 'bizarre' object has no attribute '__name__'

If you really want instances to have that also, then also do as you did.

There are other class-only, not for instances, attributes:
__mro__ and __subclasses__ and perhaps others.

----------
nosy: +terry.reedy
resolution:  -> rejected
status: open -> closed
type: behavior -> enhancement
versions:  -Python 2.6, Python 2.7, Python 3.1, Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14092>
_______________________________________


More information about the Python-bugs-list mailing list