闭包问题
def fun1(a):print('this is fun1')
def fun2(b=10):
nonlocal a
print('this is fun2')
a=a+b
return a
return fun2
i=fun1(5)
i()
执行完上面代码后输出结果为
this is fun1
this is fun2
为什么结果没有返回a+b的答案呢? def fun1(a):
print('this is fun1')
def fun2(b=10):
nonlocal a
print('this is fun2')
a=a+b
return a
return fun2
i=fun1(5)
print(i())
def fun1(a):
print('this is fun1')
def fun2(b=10):
nonlocal a
print('this is fun2')
a=a+b
print(a)
return fun2
i=fun1(5)
i()
函数fun1里面的函数fun2仅仅是返回了a的值,想要打印a还要加一个print,如2#
或者在fun2里面把返回值去掉,直接打印,如3#
欢迎追问 青出于蓝 发表于 2021-7-31 15:52
函数fun1里面的函数fun2仅仅是返回了a的值,想要打印a还要加一个print,如2#
或者在fun2里面把返回值去掉 ...
谢谢大佬,懂了懂了{:5_105:}
页:
[1]