函数嵌套与函数调用区别
点击查看代码
# 函数嵌套
x = 10
def out():
x = 20
def inner():
print(x)
return inner()
def func():
print(x)
def foo():
x = 20
func()
if __name__ == '__main__':
out() # 打印20 属于函数嵌套,当inner函数没有找到变量x时,向其上层嵌套函数中找
foo() # 打印10 属于函数调用,当func函数没有找到变量x时,在全局中找
本文来自博客园,作者:覃空万里,转载请注明原文链接:https://www.cnblogs.com/qinkwl/p/18622800
浙公网安备 33010602011771号