Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:error': 0.03; 'subject:not': 0.03; 'resulting': 0.04; 'assignment': 0.07; 'elements.': 0.07; 'sentence': 0.09; 'stack,': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; '2.7': 0.14; '"create': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'subject:key': 0.16; 'subject:when': 0.16; 'wrote:': 0.18; 'value.': 0.19; '>>>': 0.22; 'import': 0.22; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'skip:{ 20': 0.24; 'stick': 0.24; 'cc:2**0': 0.24; 'code:': 0.26; 'van': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'room': 0.29; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '2009,': 0.31; '>>>>': 0.31; 'quite': 0.32; 'fri,': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'version:': 0.36; 'doing': 0.36; 'two': 0.37; 'hat': 0.38; 'how': 0.40; 'map': 0.64; 'more': 0.64; 'different': 0.65; 'believe': 0.68; 'repeat': 0.74; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=A/uyFSpLNwKcF3+ZXcL6caKpPlNGBGFF6X/iNvhMeOA=; b=JIgBZMjORgj8WX7YpemhLE2Axz+UqARQr6qhEgU4RHdDj4k7by7RM84kYDOBxGsz9H y0KCygnG96ArOtVW5nLaXwFnyowfstynSLnueH6VOAdQb1vefpC7nFxu+9GktR+XneoV WDR/41npSMuNAuvbxzhCv94F07MW5N+qd3CJGxEs2lE7+wgGxB9FMYgKJrBK4DoYD1Tc vBDf31hr/8e8G7WGtfOENaifzr3J6stHtQONTfu0Iq7aAndX7EeuHqBIzok6tzrq/xNn OI2bJY5QVvdjdSumD86VVx/RkuJVG3gBQcEXqd7VmXxt2EQdiKy8HfRNqkGXuiRIjXkb +dxQ== MIME-Version: 1.0 X-Received: by 10.43.96.65 with SMTP id cf1mr1528959icc.26.1406845400007; Thu, 31 Jul 2014 15:23:20 -0700 (PDT) In-Reply-To: References: Date: Fri, 1 Aug 2014 08:23:19 +1000 Subject: Re: Dict when defining not returning multi value key error From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406845408 news.xs4all.nl 2936 [2001:888:2000:d::a6]:46072 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75447 On Fri, Aug 1, 2014 at 7:29 AM, Emile van Sebille wrote: > 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 Python 2.5 is quite old now, but here's the 2.7 disassembly for the same code: >>> dis.dis(test) 1 0 BUILD_MAP 2 3 LOAD_CONST 1 ('value0') 6 LOAD_CONST 2 ('k') 9 STORE_MAP 10 LOAD_CONST 3 ('value1') 13 LOAD_CONST 2 ('k') 16 STORE_MAP 17 STORE_FAST 0 (p) 20 LOAD_CONST 0 (None) 23 RETURN_VALUE I believe it's still doing the same thing. My reading of this is: "Create a map with room for two elements. Stick this value on the stack, stick this key on the stack, stow it in the map. Repeat last sentence with a different value. Now store that resulting map." So it's still just happily overriding, although the code is different from the assignment version: >>> dis.dis(test2) 1 0 BUILD_MAP 0 3 STORE_FAST 0 (p) 6 LOAD_CONST 1 ('value0') 9 LOAD_FAST 0 (p) 12 LOAD_CONST 2 ('k') 15 STORE_SUBSCR 16 LOAD_CONST 3 ('value1') 19 LOAD_FAST 0 (p) 22 LOAD_CONST 2 ('k') 25 STORE_SUBSCR 26 LOAD_CONST 0 (None) 29 RETURN_VALUE ChrisA