[Python-Dev] Exception __name__ missing?
Nick Coghlan
ncoghlan at gmail.com
Tue Jan 18 00:01:32 CET 2011
On Tue, Jan 18, 2011 at 7:09 AM, Scott Dial
<scott+python-dev at scottdial.com> wrote:
> I worked around the issue by referencing the __class__ (as the other
> replier mentioned). But, I didn't receive any responses then, so I think
> not a lot of attention was put into these type of attributes on exceptions.
That's not a workaround, it is the way you're meant to access
__module__ and __name__ on new-style classes (which was the transition
that happened for Exception in 2.5).
The fact that user-defined classes get a __module__ attribute on
instances while builtin and extension types don't isn't unique to
exceptions though:
>>> class C: pass
...
>>> C.__module__
'__main__'
>>> C().__module__
'__main__'
>>> str.__module__
'builtins'
>>> str().__module__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute '__module__'
>>> import datetime
>>> datetime.datetime.__module__
'datetime'
>>> datetime.datetime.now().__module__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute '__module__'
The addition of __module__ to user defined class instances strikes me
as a bug. You can see in the language reference [1] that __dict__ and
__class__ are the only expected data attributes for class instances.
[1] http://docs.python.org/dev/reference/datamodel.html (search for
the entry on "class instances", then scroll back up and contrast with
the sections on class objects, functions and methods)
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-Dev
mailing list