[Chicago] I don't get why this works

Jeff Hinrichs - DM&T jeffh at dundeemt.com
Tue Apr 2 05:53:06 CEST 2013


Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo():
...    a = {}
...    a['a'] = a[a['a']] = 'b'
...    return a
...
>>> import dis
>>> dis.dis(foo)
  2           0 BUILD_MAP                0
              3 STORE_FAST               0 (a)

  3           6 LOAD_CONST               1 ('b')
              9 DUP_TOP
             10 LOAD_FAST                0 (a)
             13 LOAD_CONST               2 ('a')
             16 STORE_SUBSCR
             17 LOAD_FAST                0 (a)
             20 LOAD_FAST                0 (a)
             23 LOAD_CONST               2 ('a')
             26 BINARY_SUBSCR
             27 STORE_SUBSCR

  4          28 LOAD_FAST                0 (a)
             31 RETURN_VALUE

-Jeff
slightly modified to make easier to discompile


On Mon, Apr 1, 2013 at 10:50 PM, Brantley Harris <deadwisdom at gmail.com>wrote:

> So it evaluates the right, then left to right.  That's strange.
>
>
> On Mon, Apr 1, 2013 at 10:46 PM, Alex Gaynor <alex.gaynor at gmail.com>wrote:
>
>> Sorry, I should have been more precise, first the far right hand side is
>> evaluated, so you get the string 'b' (of course). Then the assignments are
>> evaluated left to right.
>>
>> So first:
>>
>> a['a'] = 'b'
>>
>> Then
>>
>> a[a['a']] = 'b'
>>
>> Now a['a'] == 'b'
>>
>> so a['b'] = 'b'.
>>
>> Alex
>>
>>
>> On Mon, Apr 1, 2013 at 8:41 PM, Brantley Harris <deadwisdom at gmail.com>wrote:
>>
>>> Okay, then it should be a key error:
>>>
>>> > a['a'] = a[a['a']]
>>> KeyError: 'a'
>>>
>>>
>>> On Mon, Apr 1, 2013 at 10:25 PM, Alex Gaynor <alex.gaynor at gmail.com>wrote:
>>>
>>>> The assignments are going left to right.
>>>>
>>>> Alex
>>>>
>>>>
>>>> On Mon, Apr 1, 2013 at 8:24 PM, Brantley Harris <deadwisdom at gmail.com>wrote:
>>>>
>>>>> > a = {}
>>>>> > a['a'] = a[a['a']] = 'b'
>>>>> {'a': 'b', 'b': 'b'}
>>>>>
>>>>> What the what?  I would think that should KeyError, but it runs fine.
>>>>>  Someone splain.
>>>>>
>>>>> _______________________________________________
>>>>> Chicago mailing list
>>>>> Chicago at python.org
>>>>> http://mail.python.org/mailman/listinfo/chicago
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> "I disapprove of what you say, but I will defend to the death your
>>>> right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire)
>>>> "The people's good is the highest law." -- Cicero
>>>>
>>>> _______________________________________________
>>>> Chicago mailing list
>>>> Chicago at python.org
>>>> http://mail.python.org/mailman/listinfo/chicago
>>>>
>>>>
>>>
>>> _______________________________________________
>>> Chicago mailing list
>>> Chicago at python.org
>>> http://mail.python.org/mailman/listinfo/chicago
>>>
>>>
>>
>>
>> --
>> "I disapprove of what you say, but I will defend to the death your right
>> to say it." -- Evelyn Beatrice Hall (summarizing Voltaire)
>> "The people's good is the highest law." -- Cicero
>>
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> http://mail.python.org/mailman/listinfo/chicago
>>
>>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
>


-- 
Best,

Jeff Hinrichs
402.218.1473
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20130401/30d10285/attachment.html>


More information about the Chicago mailing list