Default Value

Chris Angelico rosuav at gmail.com
Fri Jun 21 22:18:56 EDT 2013


On Sat, Jun 22, 2013 at 12:01 PM, Rotwang <sg552 at hotmail.co.uk> wrote:
>>>> class hashablelist(list):
> ...     def __hash__(self):
> ...         return hash(tuple(self))

There's a vulnerability in that definition:

>>> a=hashablelist((1,[],3))
>>> a
[1, [], 3]
>>> {a:1}
Traceback (most recent call last):
  File "<pyshell#255>", line 1, in <module>
    {a:1}
  File "<pyshell#249>", line 3, in __hash__
    return hash(tuple(self))
TypeError: unhashable type: 'list'

Of course, if you monkey-patch list itself to have this functionality,
or always use hashablelist instead of list, then it will work. But
it's still vulnerable.

ChrisA



More information about the Python-list mailing list