Default Value

Rotwang sg552 at hotmail.co.uk
Fri Jun 21 22:25:46 EDT 2013


On 22/06/2013 03:18, Chris Angelico wrote:
> 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.

Quite right, sorry. I should have called my class 
"sometimeshashablelist", or something.




More information about the Python-list mailing list