hi!
I was wondering if anyone knows why I get

NameError: global name 'yclass' is not defined

on this:

x = (1, 2, 3)
class C:
    yclass = 'hello'
    ex5 = tuple((xitem, yclass) for xitem in x)

It works fine if done at module scope instead of class scope.
It also works in Python 2.6.2 if the tuple() is changed to square brackets. However Python 3.0.1 rejects it either way.

A more comprehensive example is below... 2.6.2 rejects the ERR2 line and 3.0.1 rejects both the ERR1 and ERR2 lines.

thanks for any clue you can provide... or is this a bug?

        Jonathan
--------------------------------- cut here -------------------------
x = (1, 2, 3)
ymod = 'hello'

ex1 = [(xitem, ymod) for xitem in x]
print('example 1:', ex1)

ex2 = tuple((xitem, ymod) for xitem in x)
print('example 2:', ex2)

class C:
    yclass = 'hello'

    ex3 = []
    for xitem in x:
        ex3.append((xitem, yclass))
    print('example 3:', ex3)

    ex4 = [(xitem, yclass) for xitem in x]      # -----ERR1-----
    print('example 4:', ex4)

    ex5 = tuple((xitem, yclass) for xitem in x) # -----ERR2-----
    print('example 5:', ex5)
--------------------------------- cut here -------------------------
Python 2.6.2 (r262:71600, Jun 23 2009, 12:32:53)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
('example 1:', [(1, 'hello'), (2, 'hello'), (3, 'hello')])
('example 2:', ((1, 'hello'), (2, 'hello'), (3, 'hello')))
('example 3:', [(1, 'hello'), (2, 'hello'), (3, 'hello')])
('example 4:', [(1, 'hello'), (2, 'hello'), (3, 'hello')])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 10, in <module>
    class C:
  File "test.py", line 21, in C
    ex5 = tuple((xitem, yclass) for xitem in x) # -----ERR2-----
  File "test.py", line 21, in <genexpr>
    ex5 = tuple((xitem, yclass) for xitem in x) # -----ERR2-----
NameError: global name 'yclass' is not defined
>>>

Reply via email to