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

Reply via email to