A newbie quesiton: local variable in a nested funciton

jfong at ms4.hinet.net jfong at ms4.hinet.net
Sat Dec 26 23:11:12 EST 2015


Last night I noticed that Python does not resolve name in "def" during import, as C does in the compile/link stage, it was deferred until it was referenced (i.e. codes was executed). That's OK for Anyway codes has to be debugged sooner or later. I just have to get used to this style.

But check these codes, it seems not.
-------
x = 1  # a global variable
print(x)

class Test:
    x = 4  # a class attribute
    print(x)
    def func(self):
        print(x)

x1 = Test()
x1.x = 41  # a instance's attribute
x1.func()  # it's 1 but 41 was expect:-(
--------

--Jach



More information about the Python-list mailing list