Message288578
Reported from [1] and similar to issue11205
Currently the evaluation order for keys and values in a dictionary comprehension follows that of assignments. The values get evaluated first and then the keys:
def printer(v):
print(v, end=' ')
return v
d = {printer(i): printer(j) for i, j in [(1, 2), (3, 4)]}
# 2 1 4 3
This seems to conflict with the semantics as described in the Semantics section of PEP 274 [2] and according to my interpretation of the reference manual (I'd expect the evaluation to be similar to dict-displays).
How should this be addressed? Fix the evaluation order or specify this edge case an "Implementation detail" in the reference manual?
I already have a fix for this lying around (changes to `compiler_sync_comprehension_generator`, `compiler_sync_comprehension_generator` and a switch in `MAP_ADD`) and can make a pull request if required.
I'm not sure if this is classified as a bug per-se so I only tagged Py3.7 for it.
[1] http://stackoverflow.com/questions/42201932/order-of-operations-in-a-dictionary-comprehension
[2] https://www.python.org/dev/peps/pep-0274/#semantics |
|
Date |
User |
Action |
Args |
2017-02-25 19:54:43 | Jim Fasarakis-Hilliard | set | recipients:
+ Jim Fasarakis-Hilliard |
2017-02-25 19:54:43 | Jim Fasarakis-Hilliard | set | messageid: <1488052483.04.0.021043093911.issue29652@psf.upfronthosting.co.za> |
2017-02-25 19:54:43 | Jim Fasarakis-Hilliard | link | issue29652 messages |
2017-02-25 19:54:42 | Jim Fasarakis-Hilliard | create | |
|