Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: lists and tuples (http://www.velocityreviews.com/forums/t318959-re-lists-and-tuples.html)

Fredrik Lundh 06-28-2003 01:56 PM

Re: lists and tuples
 
Gerrit Holl wrote:

> This is probably a basic question, but I can't see why mutable
> objects can't be used as dictionairy keys. Why would this be
> impossible:
>
> 0 >>> d = {}
> 1 >>> l = []
> 2 >>> d[l] = "foo"
> 3 >>> d
> {[]: 'foo'}
> 4 >>> l.append("bar")
> 5 >>> l
> ['bar']
> 6 >>> d
> {['bar']: 'foo'}
>
> What is the problem of doing this?


it's explained in the Python language reference:

The only types of values not acceptable as keys are values
containing lists or dictionaries or other mutable types that
are compared by value rather than by object identity, the
reason being that the efficient implementation of dictionaries
requires a key's hash value to remain constant.

(anyone thinking of following up with "but use a tree" or "make all
objects observable" etc should check the archives first (or write
their own Python-compatible-but-slower language, and compete
in the marketplace...))

</F>






All times are GMT. The time now is 04:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57