|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> def out(m):
def inner(n):
n=m+n
return n
return inner
>>> l = out(3)
>>> type(l)
<class 'function'>
>>> l(5)
8
如上,这里是return inner后没有括号也没有报错,如果随便弄一个没定义过的字母就会报错
这个innner是被当作函数了吗,和 return inner()作用貌似是一样的??
>>> def a():
m=1
def b():
m += 1
return m
return b()
>>> a()
Traceback (most recent call last):
File "<pyshell#72>", line 1, in <module>
a()
File "<pyshell#71>", line 6, in a
return b()
File "<pyshell#71>", line 4, in b
m += 1
UnboundLocalError: local variable 'm' referenced before assignment
如上就报错了,但是下面
>>> def a():
m=1
def b():
m += 1
return m
return b
>>> a()
<function a.<locals>.b at 0x00853540>
又不会报错了
这两种情况都会报错。错误的原因是定以前引用。
让程序2完全执行就会显出错误。a()()
如果在程序中的def b()中加入 nonlocal m就正确了
|
|