[issue31126] dict comprehension shouldn't raise UnboundLocalError

2017-08-06 Thread Eryk Sun
Eryk Sun added the comment: It's consistent with the behavior of generator expressions: Variables used in the generator expression are evaluated lazily when the __next__() method is called for the generator object (in the same fashion as normal generators). However, the leftmost

[issue31126] dict comprehension shouldn't raise UnboundLocalError

2017-08-06 Thread R. David Murray
R. David Murray added the comment: I wonder if that explanation should be added to the doc section to which I pointed. I thought I'd remembered something like that being in there, but it isn't. -- ___ Python tracker

[issue31126] dict comprehension shouldn't raise UnboundLocalError

2017-08-06 Thread Eryk Sun
Eryk Sun added the comment: Comprehensions evaluate the iterator for the outermost loop in the surrounding scope. The iterators for all inner loops are evaluated in the local scope of the comprehension itself. -- nosy: +eryksun resolution: -> not a bug stage: -> resolved status:

[issue31126] dict comprehension shouldn't raise UnboundLocalError

2017-08-06 Thread R. David Murray
R. David Murray added the comment: The behavior is consistent: >>> a = [1, 2] >>> b = [3, 4] >>> [(a, b) for a in a for b in b] Traceback (most recent call last): File "", line 1, in File "", line 1, in UnboundLocalError: local variable 'b' referenced before assignment I'm not sure why

[issue31126] dict comprehension shouldn't raise UnboundLocalError

2017-08-06 Thread ksqsf
New submission from ksqsf: The code key = ["a", "b"] val = [1, 2] dic = {key:val for key in key for val in val} will raise UnboundLocalError in Python 3.6.2 and 2.7.13. Intuitively, the element 'key' and the list 'key' are not the same, so generally the expected result is {"a": 1,