我已经简要地浏览了几十个关于UnboundLocalErrorpython 嵌套函数的qus ,我以为我理解了,直到我用自定义类实例尝试它
def fn1():
a = 1
def fn1a():
print(a)
a += 1
print(a)
fn1a()
class B:
v = 1
def fn2():
b = B()
def fn2a():
print(b.v)
b.v += 1
print(b.v)
fn2a()
fn2() # prints 1 then 2
fn1() # raises UnboundLocalError
为什么错误没有被提出fn2?
慕姐4208626
相关分类