[New-bugs-announce] [issue29652] Fix evaluation order of keys/values in dict comprehensions

Jim Fasarakis-Hilliard report at bugs.python.org
Sat Feb 25 14:54:43 EST 2017


New submission from Jim Fasarakis-Hilliard:

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

----------
components: Interpreter Core
messages: 288578
nosy: Jim Fasarakis-Hilliard
priority: normal
severity: normal
status: open
title: Fix evaluation order of keys/values in dict comprehensions
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29652>
_______________________________________


More information about the New-bugs-announce mailing list