[Chicago] I don't get why this works
Scott Sexton
ssexton at nectarlab.com
Tue Apr 2 05:53:33 CEST 2013
All these are equivalent: a['a'] = a[a['a']] = 'b'
So that gives you two equalities:
1: a['a'] = 'b'
2: a[a['a']] = 'b'.
It's clear that for a['a'] = 'b', you'd expect {'a':'b'}.
Now in a[a['a']] = 'b', you can substitute the inside a['a'] (equality 1
above) for its value 'b'.
This gives you a['b'] = b
So that's why you get {'a':'b', 'b':'b'}
On Mon, Apr 1, 2013 at 10: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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20130401/86f0a33c/attachment.html>
More information about the Chicago
mailing list