On 7/31/2014 2:16 PM, Ian Kelly wrote:
On Thu, Jul 31, 2014 at 1:17 PM, Terry Reedy <tjre...@udel.edu> wrote:
On 7/31/2014 7:24 AM, Dilu Sasidharan wrote:

I am wondering why the dictionary in python not returning multi value
key error when i define something like

p = {'k':"value0",'k':"value1"}


This is documented behavior: "you can specify the same key multiple times in
the key/datum list, and the final dictionary’s value for that key will be
the last one given." I am not sure whether this is an accident of the
initial design, never changed since, or intended for certain uses.  It may
partly be because this choice is slightly simpler or, since keys are
expressions, not constants, that the check can only come at runtime.

I don't know either, but I think that it's generally expected that the
above would be equivalent to:

p = dict()
p['k'] = "value0"
p['k'] = "value1"

which of course will not throw an error since you're just updating the
value in that case.

And which is also how it works:

Python 2.5 (r25:51908, Dec 18 2009, 14:22:21)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> def test():p = {'k':"value0",'k':"value1"}
...
>>> from dis import dis
>>> dis(test)
  1           0 BUILD_MAP                0
              3 DUP_TOP
              4 LOAD_CONST               1 ('value0')
              7 ROT_TWO
              8 LOAD_CONST               2 ('k')
             11 STORE_SUBSCR
             12 DUP_TOP
             13 LOAD_CONST               3 ('value1')
             16 ROT_TWO
             17 LOAD_CONST               2 ('k')
             20 STORE_SUBSCR
             21 STORE_FAST               0 (p)
             24 LOAD_CONST               0 (None)
             27 RETURN_VALUE


Emile


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to