I have been baffled by an odd behaviour of Python locals()
.
Basically I want to get an item from the dictionary of locals()
in a dictionary comprehension, yet it fails. It is an extremely basic thing, so:
>>> foo=123
>>> bar=345
>>> baz=678
>>> {k: locals()[k] for k in ('foo','bar','baz')}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <dictcomp>
KeyError: 'foo'
>>> locals()['foo']
123
>>> locale=locals()
>>> {k: locale[k] for k in ('foo','bar','baz')}
{'foo': 123, 'bar': 345, 'baz': 678}
>>> type(locals())
<class 'dict'>
>>> def fun():
... return {'foo': 123,'bar':345}
...
>>> {k: fun()[k] for k in ('foo','bar')}
{'foo': 123, 'bar': 345}
On a practical side the uglier {'foo':foo, 'bar': bar}
etc. in dict or in string .format()
works fine.
It is just that I am missing something so knowing why will increase my coding chi (as of now I don't glow when coding).
Because all comprehensions in Python 3 are implemented using a hidden function, calling locals
doesn't return the values you're expecting it to return.
You can see this by printing the values out:
>>> _ = {k: print(locals()) for k in ('foo','bar','baz')}
{'k': 'foo', '.0': <tuple_iterator object at 0x7fdf840afa90>}
{'k': 'bar', '.0': <tuple_iterator object at 0x7fdf840afa90>}
{'k': 'baz', '.0': <tuple_iterator object at 0x7fdf840afa90>}
Assigning locals()
to locale
, as you do, takes care of this. You aren't calling locals
inside the comprehension.
Note that, in Python 2, the situation is a bit murkier. dict-comps do fail in a similar fashion but list-comps, which predate dict-comps, work just fine:
>>> _ = [locals()[k] for k in ('foo', 'bar', 'baz')]
>>> _
[20, 40, 60]
this is yet another 'wart' that was addressed with Py3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With