Message344948
More ways to be bitten by this strange behavior:
>>> d = {'a': 1, 'b': 2}
>>> eval('[x[k] for k in x]', {}, {'x': d})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
File "<string>", line 1, in <listcomp>
NameError: name 'x' is not defined
>>>
>>> def f():
... x = {'a': 1, 'b': 2}
... return eval('[x[k] for k in x]')
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in f
File "<string>", line 1, in <module>
File "<string>", line 1, in <listcomp>
NameError: name 'x' is not defined
In both of the above cases changing
eval('[x[k] for k in x]')
to
eval('[v for v in x.values()]')
avoids the problem. There are no problems when using
[x[k] for k in x]
without `eval()` either. I'd prefer this to be changed, but there should at least be a note in the documentation of `eval()` about this. |
|
Date |
User |
Action |
Args |
2019-06-07 14:54:57 | pekka.klarck | set | recipients:
+ pekka.klarck, terry.reedy, eamanu, Martin Hosken |
2019-06-07 14:54:57 | pekka.klarck | set | messageid: <1559919297.95.0.617559323573.issue36300@roundup.psfhosted.org> |
2019-06-07 14:54:57 | pekka.klarck | link | issue36300 messages |
2019-06-07 14:54:57 | pekka.klarck | create | |
|