[Python-Dev] Mundane dict __setitem__...

David Abrahams dave@boost-consulting.com
2003年6月03日 11:17:05 -0400


Guido van Rossum <guido@python.org> writes:
>> I am about to write some code which relies on the following:
>>>> x = (1,2,3)
>> y = (1,2,3)
>> d = {}
>> d[x] = 1
>> d[y] = 1
>> assert d.keys()[0] is y
>>>> In other words, when you do d[k] = v it replaces both v *and* k in the
>> dict. I couldn't find it documented anywhere but the source. I
>> submitted a doc patch which enshrines that behavior, but since Guido
>> doesn't always know what's in the doc I wanted to make sure it was
>> considered reasonable.
>>>> Anyone want to tell me it's a bad idea? It seems like the more useful
>> of the two possible behaviors to me.
>> Bad idea. There are no guarantees whether the key will be updated or
> not.

Too bad. It's better to update the key.
> (There is also no rule that forbids the compiler from making x
> and y point to the same object, BTW.)

I knew that part. Note that my test doesn't rely on having distinct
identities.
-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

AltStyle によって変換されたページ (->オリジナル) /