NameError assigning to class created in a function

Andrew Bennetts andrew-pythonlist at puzzling.org
Wed Mar 20 00:53:00 EST 2002


(Tested on Python 2.2)

This fails:
def f(x):
    class Private:
        x = x
    return Private
x(1)

but this works:
def f(x):
    y = x
    class Private:
        x = y
    return Private
x(1)

but this also fails:
def f(x):
    y = x
    class Private:
        y = y
    return Private
x(1)

So, it seems that you can't assign to a variable in a class if it has the
same name as the variable you are assigning from.  Except that it works
outside a function:
x = 1
class C:
    x = x

It's easy enough to workaround, but annoying.  Is this a genuine bug?  If
so, I'll submit it to the SF bug tracker...

-Andrew.

(Please CC: me as I'm not currently subscribed to the list/newsgroup)




More information about the Python-list mailing list