[issue31126] dict comprehension shouldn't raise UnboundLocalError

ksqsf report at bugs.python.org
Sun Aug 6 09:17:16 EDT 2017


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, "b": 2}.

There are similar cases for listcomps, setcomps and genexprs:
    l = [1, 2, 3]
    {l for l in l}   # => {1, 2, 3}
    [l for l in l]   # => [1, 2, 3]
    for l in (l for l in l):
        print(l, end=' ')
                     # => 1 2 3 
All of them do as what is expected. 

For consistency and intuitiveness, the behavior of distcomps should be modified so that no UnboundLocalError is raised.

----------
components: Interpreter Core
messages: 299799
nosy: ksqsf
priority: normal
severity: normal
status: open
title: dict comprehension shouldn't raise UnboundLocalError
type: behavior
versions: Python 3.6

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


More information about the Python-bugs-list mailing list