[issue18110] Nested set comprehensions in class scope fail

Eric Wieser report at bugs.python.org
Sat Jun 1 00:22:08 CEST 2013


New submission from Eric Wieser:

This code:

    class Sudoku(dict):
        COLUMNS = [
            {(x, y) for y in xrange(9)} for x in xrange(9)
        ]

When run on Python 2.7.5, fails with this traceback:

    Traceback (most recent call last):
      File "<pyshell#3>", line 1, in <module>
        class Sudoku(object):
      File "<pyshell#3>", line 3, in Sudoku
        {(x, y) for y in xrange(9)} for x in xrange(9)
      File "<pyshell#3>", line 3, in <setcomp>
        {(x, y) for y in xrange(9)} for x in xrange(9)
    NameError: global name 'x' is not defined

Yet `x` is clearly not a global - it's defined in the comprehension.

Running the comprehension outside of the class gives no error:

    >>> [
        {(x, y) for y in xrange(9)} for x in xrange(9)
    ]
    [set([...]), ...]

Nor does using `set`:

    class Sudoku(dict):
        COLUMNS = [
            set((x, y) for y in xrange(9)) for x in xrange(9)
        ]

----------
components: Interpreter Core
messages: 190420
nosy: Eric.Wieser
priority: normal
severity: normal
status: open
title: Nested set comprehensions in class scope fail
type: behavior
versions: Python 2.7

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


More information about the Python-bugs-list mailing list