Why are types not hashable?
Chris Liechti
cliechti at gmx.net
Tue May 28 16:51:59 EDT 2002
VanL <vlindberg at verio.net> wrote in news:3CF3E7A4.9010103 at verio.net:
> What prevents this from working:
>
> >>> import types
> >>> alltypes = [t for t in types.__dict__.keys() if t[:2] != '__']
> >>> typestrings ={}
> >>> for t in alltypes: typestrings[eval('types.' + t): t]
no need for "eval" here... a) there is dict.items() b) there is getattr
> ...
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: unhashable type
>
>
> Why is <type 'dict'> not hashable? Surely its not mutable?
it is (py 2.2)...
>>> d = {}
>>> for k,v in types.__dict__.items():
... if k[-4:] == 'Type':
... d[v]=k
...
or if you want as a one liner in functional style:
d = dict([ (v,k) for k,v in types.__dict__.items() if k[-4:] == 'Type'])
>>> d[int]
'IntType'
>>> d[dict]
'DictType'
>>> dict
<type 'dict'>
>>>
chris
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list