Re: (objects as) mutable dictionary keys

2005-01-17 Thread Just
In article <[EMAIL PROTECTED]>, Peter Maas <[EMAIL PROTECTED]> wrote: > Antoon Pardon schrieb: > > Dictionary lookup with mutable types like lists is a source of > > unpleasant surprises for the programmer and therefore impossible in > > Python. > > > > > > It is not impossible in Python.

Re: (objects as) mutable dictionary keys

2005-01-17 Thread Peter Maas
Antoon Pardon schrieb: Dictionary lookup with mutable types like lists is a source of unpleasant surprises for the programmer and therefore impossible in Python. It is not impossible in Python. It may be discouraged but it is not impossible since I have already done so. Wouldn't this raise a

Re: (objects as) mutable dictionary keys

2005-01-17 Thread Nick Coghlan
Antoon Pardon wrote: What are good arguments or bad and how much weight they have depends on the person and on the circumstances. So a simple rule like: Never use a mutable as a key in a dictionary will sometimes not be the best solution. True - but I think a purely identity based dictionary *is* t

Re: (objects as) mutable dictionary keys

2005-01-17 Thread Antoon Pardon
Op 2005-01-14, Steve Holden schreef <[EMAIL PROTECTED]>: > Antoon Pardon wrote: > >> Op 2005-01-14, Peter Maas schreef <[EMAIL PROTECTED]>: >> >>>I have summarized the discussion about the usability of lists (and >>>and other mutable types) as dictionary keys and put it into the >>>Python wiki.URL

Re: (objects as) mutable dictionary keys

2005-01-14 Thread Steven Bethard
Peter Maas wrote: I have summarized the discussion about the usability of lists (and and other mutable types) as dictionary keys and put it into the Python wiki.URL: http://www.python.org/moin/DictionaryKeys. Antoon Pardon wrote: > I had a look and I think you should correct the followingr: > > D

Re: (objects as) mutable dictionary keys

2005-01-14 Thread Steve Holden
Antoon Pardon wrote: Op 2005-01-14, Peter Maas schreef <[EMAIL PROTECTED]>: I have summarized the discussion about the usability of lists (and and other mutable types) as dictionary keys and put it into the Python wiki.URL: http://www.python.org/moin/DictionaryKeys. This summary might be used as a

Re: (objects as) mutable dictionary keys

2005-01-14 Thread John Roth
"Peter Maas" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] I have summarized the discussion about the usability of lists (and and other mutable types) as dictionary keys and put it into the Python wiki.URL: http://www.python.org/moin/DictionaryKeys. This summary might be used as a re

Re: (objects as) mutable dictionary keys

2005-01-14 Thread Antoon Pardon
Op 2005-01-14, Peter Maas schreef <[EMAIL PROTECTED]>: > I have summarized the discussion about the usability of lists (and > and other mutable types) as dictionary keys and put it into the > Python wiki.URL: http://www.python.org/moin/DictionaryKeys. > > This summary might be used as a reference s

Re: (objects as) mutable dictionary keys

2005-01-14 Thread Peter Maas
I have summarized the discussion about the usability of lists (and and other mutable types) as dictionary keys and put it into the Python wiki.URL: http://www.python.org/moin/DictionaryKeys. This summary might be used as a reference should the 'mutable dictionary keys' issue come up again in c.l.py

Re: objects as mutable dictionary keys

2004-12-29 Thread Nick Coghlan
Terry Reedy wrote: "Nick Coghlan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] This *is* a bug (since Guido called it such), but one not yet fixed as the obvious solution (removing object.__hash__) causes problems for Jython, and a non-obvious solution has not been identified. S

Re: objects as mutable dictionary keys

2004-12-29 Thread Terry Reedy
"Nick Coghlan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > This *is* a bug (since Guido called it such), but one not yet fixed as > the obvious solution (removing object.__hash__) causes problems for > Jython, > and a non-obvious solution has not been identified. Since object

Re: objects as mutable dictionary keys

2004-12-29 Thread Stian Søiland
On 2004-12-29 14:04:19, Nick Coghlan wrote: > This *is* a bug (since Guido called it such), but one not yet fixed as the > obvious solution (removing object.__hash__) causes problems for Jython, and > a non-obvious solution has not been identified. class object: def __hash__(self):

Re: objects as mutable dictionary keys

2004-12-29 Thread Nick Coghlan
Yeah, that looks like a pretty decent summary to me. I wonder if this would be worth posting on the Wiki somewhere? Maybe: http://www.python.org/moin/DictionaryKeys That makes it easy to point to when this issue comes up again. The page may also want to mention an extant bug with new-style clas

Re: objects as mutable dictionary keys

2004-12-28 Thread Steven Bethard
Peter Maas wrote: Peter Maas schrieb: There was a huge and sometimes heated debate about tuples, lists and dictionaries recently, and the mainstream opinion was that dictionary keys must not be mutable, so lists are not allowed as dictionary keys. Warning, long posting (~ 100 lines) [snip summary]

Re: objects as mutable dictionary keys

2004-12-28 Thread John Roth
I think that's a good summary. The condensed version is that the results of both __hash__() and __cmp__() have to remain stable for dicts to work as one would expect. __cmp__ doesn't do that for lists, and it isn't defined by default for user objects. John Roth "Peter Maas" <[EMAIL PROTECTED]> wrot

Re: objects as mutable dictionary keys

2004-12-28 Thread Peter Maas
Peter Maas schrieb: There was a huge and sometimes heated debate about tuples, lists and dictionaries recently, and the mainstream opinion was that dictionary keys must not be mutable, so lists are not allowed as dictionary keys. Warning, long posting (~ 100 lines) The existence of lists and tuples

Re: objects as mutable dictionary keys

2004-12-28 Thread Peter Maas
John Roth schrieb: No. The basic answer is that it's up to the object whether it will allow itself to be used as a dictionary key. In other words, if the designer of an object thinks it makes sense for instances to be dictionary keys, then he can supply a __hash__() method. If he doesn't, then he d

Re: objects as mutable dictionary keys

2004-12-27 Thread Steven Bethard
Peter Maas wrote: Steven Bethard schrieb: If lists were hashable, new programmers to Python would almost certainly make mistakes like: py> d = {[1, 2, 3]: 'abc'} > The coder here almost certainly *doesn't* want that list to be compared > by id. The only way to get a binding for that list woul

Re: objects as mutable dictionary keys

2004-12-27 Thread Andrew Koenig
"Peter Maas" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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 co

Re: objects as mutable dictionary keys

2004-12-27 Thread John Roth
"Peter Maas" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] There was a huge and sometimes heated debate about tuples, lists and dictionaries recently, and the mainstream opinion was that dictionary keys must not be mutable, so lists are not allowed as dictionary keys. BUT: objects ar

Re: objects as mutable dictionary keys

2004-12-27 Thread Peter Maas
Steven Bethard schrieb: If lists were hashable, new programmers to Python would almost certainly make mistakes like: py> d = {[1, 2, 3]: 'abc'} > The coder here almost certainly *doesn't* want that list to be compared > by id. The only way to get a binding for that list would be using the > dict

Re: objects as mutable dictionary keys

2004-12-27 Thread Andrew Dalke
Andrew Koenig: > If d is a dict and t1 and t2 are tuples, and t1 == t2, then d[t1] and d[t2] > are the same element. So long as the elements of t1 and t2 are well-behaved. >>> class Spam: ... def __hash__(self): ... return id(self) ... def __eq__(self, other): ... return True ... >>

Re: objects as mutable dictionary keys

2004-12-27 Thread Peter Maas
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 objec

Re: objects as mutable dictionary keys

2004-12-27 Thread Steven Bethard
Peter Maas wrote: 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 ha

Re: objects as mutable dictionary keys

2004-12-27 Thread Andrew Koenig
"Peter Maas" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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 unhashab