Why are types not hashable?

Andrew Dalke dalke at dalkescientific.com
Tue May 28 17:01:56 EDT 2002


VanL:
> >>> import types
> >>> alltypes = [t for t in types.__dict__.keys() if t[:2]
>!= '__']

BTW, use dir(types)

> >>> typestrings ={}
> >>> for t in alltypes: typestrings[eval('types.' + t): t]
>...

BTW, use getattr(types, t)

I can't reproduce what you get on my machine.  For example,
what is
  typestrings[eval('types.' + t): t]
?  It looks like a slice, as in
  typestrings[types.DictionaryType: "DictionaryType"]

(Trying this on 1.5.2 yields 'TypeError: slice index must be int'
 .. a-ha!  Under 2.2 it yields your exception)
when I think you want

  typestrings[getattr(types, t)] = t

>Why is <type 'dict'> not hashable?  Surely its not mutable?

A simpler way to test if that is the problem is

a = {}
a[type(a)] = "Spam"

Works just fine for me under both Python versions.

                    Andrew







More information about the Python-list mailing list