|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> def funX(x):
... def funY(y):
... return x*y
... return funY
...
>>> funX(4)
<function funX.<locals>.funY at 0xf7bd04f4>
>>> type(funX(4))
<class 'function'>
>>> funX(4)(7)
28
>>> def funX(x):
... def funY(y):
... return x*y
... return funY()
...
>>> funX(4)(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in funX
TypeError: funY() missing 1 required positional argument: 'y'
>>> def funX(x):
... def funY(y):
... return x*y
... return funY(y)
...
>>> funX(4)(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in funX
NameError: name 'y' is not defined
为什么后两种的函数不能成功?能给分析一下吗?谢谢!
1.后面的函数x中你错误地引用了函数y的形参,然而这个形参不再x函数的作用域中。
2.可以,但是你第二个是错的。
|
|