Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscripting locals() in a dict comprehension fails with KeyError [duplicate]

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).

like image 640
Matteo Ferla Avatar asked Oct 21 '17 11:10

Matteo Ferla


1 Answers

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.

like image 62
Dimitris Fasarakis Hilliard Avatar answered Oct 06 '22 00:10

Dimitris Fasarakis Hilliard