[Python-Dev] Exception __name__ missing?
Ron Adam
rrr at ronadam.com
Tue Jan 18 03:41:32 CET 2011
On 01/17/2011 02:27 PM, Georg Brandl wrote:
> Am 17.01.2011 21:22, schrieb Ron Adam:
>>
>> Is this on purpose?
>>
>>
>> Python 3.2rc1 (py3k:88040, Jan 15 2011, 18:11:39)
>> [GCC 4.4.5] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> Exception.__name__
>> 'Exception'
>> >>> e = Exception('has no name')
>> >>> e.__name__
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in<module>
>> AttributeError: 'Exception' object has no attribute '__name__'
>
> It's not on purpose in the sense that it's not something special
> to exceptions. The class __name__ attribute is not accessible
> from instances of any class.
Yes, I realised this on the way to an appointment. Oh well. ;-)
What I needed was e.__class__.__name__ instead of e.__name__.
I should have thought about this a little more before posting.
The particular reason I wanted it was to format a nice message for
displaying in pydoc browser mode. The server errors, like a missing .css
file, and any other server related errors, go the server console, while the
content errors get displayed in a web page. ie... object not found, or
some other content related reason for not giving what was asked for.
Doing repr(e) was giving me too much.
UnicodeDecodeError('utf8', b'\x7fELF\x02\x01\x01\x00\x00\x00\x ....
With pages of bytes, and I'd rather not truncate it, although that would be ok.
str(e) was more useful, but didn't include the exception name.
'utf8' codec can't decode byte 0xe0 in position 24: invalid
continuation byte
So doing e.__name__ was the obvious next thing... for some reason I
expected the __name__ attribute in exception instances to be inherited from
the class. Beats me why. <shrug>
Thanks,
Ron
More information about the Python-Dev
mailing list