[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: Re: objects as mutable dictionary keys
From: Peter Maas <peter () somewhere ! com>
Date: 2004-12-27 22:52:37
Message-ID: 33bi4tF3u4cilU1 () individual ! net
[Download RAW message or body]
Andrew Koenig schrieb:
>>This strikes me because if one can do this with instances of user
>>defined classes why not with lists? Trying to use lists as dict
>>keys yields "TypeError: list objects are unhashable". So why are
>>list objects unhashable and user defined objects hashable? For
>>user defined objects hash(x1) = id(x1), why not do the same
>>with lists?
>
>
> If d is a dict and t1 and t2 are tuples, and t1 == t2, then d[t1] and d[t2]
> are the same element.
>
> If lists used the id as the hash, this property would not hold for lists.
This leads to the question:
Why does (t1 == t2 => d[t1] identical to d[t2]) hold for user defined
objects and not for lists? My answer: because the cmp function looks at
id() for user defined objects and at list content for lists.
Why does the cmp function _have_ to look at lists contents?
My answer: because of the existence of list literals.
> Can you give me an example of a program for which you consider such behavior
> to be useful?
I'm not interested in using lists as dict keys. I was just searching
for an explanation why user defined objects can be used as dict keys
contrary to lists.
--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------
--
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic