![]() |
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.