Tuples -- who needs 'em

Mike Fletcher mfletch at tpresence.com
Tue Apr 4 09:13:15 EDT 2000


I think the point that is being made is this:
	The _value_ of a mutable object cannot (easily) be used as a
dictionary key...
	i.e. in this sequence, what is the value of the final line...

s = [ 1,2,3 ]
p = { s: 4, [2,3,4]: 5 }
s[:] = [2,3,4]
p[ [2,3,4] ]

You can get around that by saying (effectively):
	When a mutable object's value is used as a dictionary key, a copy of
the object is used, rather than the object itself (that is, further changes
to the object do not alter the key mapping.

Which is done, today, as:
	p = { tuple(s):4, tuple([2,3,4]):5 }

The problem is, you still don't have anything like what you really want
(that is, store the objects by the keys and somehow do magic behind the
scenes to decide which instance is going to be "the one" when keys are
equal.  Rigorously define that in a simple-to-understand and teach way, with
plausible implementation details, and you might get the powers to agree, no?

Of course, you could always use id( listvalue) as the key, but that doesn't
have the value, only the identity of the object, so two equal lists won't
give the same key.

Enjoy all,
Mike

-----Original Message-----
From: Hrvoje Niksic [mailto:hniksic at iskon.hr]
Sent: Tuesday, April 04, 2000 9:04 AM
To: python-list at python.org
Subject: Re: Tuples -- who needs 'em


"Fredrik Lundh" <effbot at telia.com> writes:

> -- tuples can be used as dictionary keys.  mutable collections can
>    not.

This is wrong.
...




More information about the Python-list mailing list