[issue7659] Attribute assignment on object() instances raises wrong exception

Nick Coghlan report at bugs.python.org
Thu Jan 27 05:05:49 CET 2011


Nick Coghlan <ncoghlan at gmail.com> added the comment:

Object instances don't get a __dict__ by default to save the space the pointer and dict instance would require. Most builtins are missing that pointer. It is also the main memory saving provided by the use of __slots__.

As far as AttributeError vs TypeError goes, the CPython core is actually pretty arbitrary as to which it raises (usually for historical reasons). In this case, AttributeError is technically correct, since object.__setattr__ *does* support writable attributes. It just so happens that a bare object() instance doesn't have any (since __doc__ is read-only).

I asked Guido ages ago about cleaning some of this up (due to some discrepancies between __enter__ and __exit__ and other special methods), but he was of the opinion that the backwards compatibility hassles weren't worth the gain in consistency.

----------
nosy: +ncoghlan

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


More information about the Python-bugs-list mailing list