exception question

Bruno Desthuilliers bdesth.nospam at removeme.free.fr
Mon Sep 1 00:23:35 CEST 2003


Gonçalo Rodrigues wrote:
> Hi,
> 
> For error processing I found convenient maintaining a dictionary where
> the keys are exception *classes* and the values are callables. Of
> course, for this to work, exception classes have to be hashable which
> I happily found that they were. So my question is, can I count on this
> behaviour? Or is this behaviour I should not count on? (I found
> nothing on the docs about it, thus the question).

Did you try ? I guess you didn't :

Python 2.2.2 (#2, Feb  5 2003, 10:40:08)
[GCC 3.2.1 (Mandrake Linux 9.1 3.2.1-5mdk)] on linux-i386
Type "help", "copyright", "credits" or "license" for more information.
 >>> class TestError(Exception): pass
...
 >>> d = {TestError: "TestError"}
 >>> d
{<class __main__.TestError at 0x80a66bc>: 'TestError'}
 >>> l = [1,2,3]
 >>> d = {l : "truc"}
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: list objects are unhashable
 >>>

BTW, it seems that classes are hashable !-)

Bruno





More information about the Python-list mailing list