PEP 218 Re: ANN: set-0.1 module available

Bernhard Herzog bh at intevation.de
Sat May 18 13:56:40 EDT 2002


bokr at oz.net (Bengt Richter) writes:

> On 18 May 2002 17:15:29 +0200, Bernhard Herzog <bh at intevation.de> wrote:
> 
> >It seems to me that the most sensible solution would be to deal with it
> >like a dictionary deals with mutable keys: By not caring about whether
> >an object is mutable or not (there is no way to determine this anyway)
> >but relying on a certain behavior regarding hash values instead.
> 
>  >>> d={}
>  >>> l=range(5)
>  >>> d[l]='list5'
>  Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
>  TypeError: list objects are unhashable
>  >>> l2=tuple([[x] for x in xrange(5)])
>  >>> l2
>  ([0], [1], [2], [3], [4])
>  >>> d[l2] = 'tuplist5'
>  Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
>  TypeError: list objects are unhashable

The messages don't say "list objects are mutable" they say they're
unhashable. This actually demonstrates what I was trying to say. The
objects used as keys have to be hashable and their hashvalues have to
follow certain rules but the objects may well be mutable.

You can easily create a sub-class of list that can be used a key in a
dict, for instance (similar things work in older versions of python as
well):

Python 2.2 (#1, Jan 17 2002, 21:04:07) 
[GCC 2.95.4 20011006 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class mylist(list):
...     def __eq__(self, other):
...             return self is other
...     def __hash__(self):
...             return id(self) 
... 
>>> m = mylist()
>>> d = {m: "abc"}
>>> d
{[]: 'abc'}
>>> m.append(1)
>>> d
{[1]: 'abc'}
>>> 

> It seems to care "about whether an object is mutable or not" and doesn't
> seem to believe that "(there is no way to determine this anyway)".

There is now way in python to tell whether any given object is mutable
or not for all objects. Whether an object is immutable depends solely on
its implementation. There is no flag in the object or type structures
that define whether an object is mutable or not or any other standard
way to detect this.

> You must have meant something else?

No, I meant what I said.

> >If an object is stored in a dict as a key, the only things that matter
> >are that
> >
> >1. its hash value doesn't change as long as the object is used as a key.
> >
> >2. equal objects have equal hash values
> >
> for some definition of hashing an object equality...

Not sure what you mean by that, but 2. above means that a == b implies
hash(a) == hash(b)

> >There may be some more subtle requirements. I'm not sure what happens
> >when two objects that have the same hash value but do not compare eqal
> >at first later become equal, for instance.
> >
> You're not confusing names and values, are you?

No. I was talking about objects being used as keys in dicts.


   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/



More information about the Python-list mailing list