首先进行分析:
1.n对于strToN是局部变量,当strToN调用结束后,理论上就会被释放;
2.n对于func是外部变量,strToInt指向func函数,
3.func函数不会释放,n就被作为外部变量存储到了func中
验证代码:、
def strToN(n):
def func(s):
s = s.strip()
print('in func locals():',locals())
return int(s, n)
return func
strToInt = strToN(10)
strToHex = strToN(16)
print(strToInt(' 10 '))结果显示:
in func locals(): {'s': '10', 'n': 10}
10