|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
return 返回的函数funy只是多带了一个括号,为什么会出错?
>>> def funx(x):
def funy(y):
return x * y
return funy()
>>> i = funx(8)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
i = funx(8)
File "<pyshell#1>", line 4, in funx
return funy()
TypeError: funy() missing 1 required positional argument: 'y'
>>> i = funx(8)(5)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
i = funx(8)(5)
File "<pyshell#1>", line 4, in funx
return funy()
TypeError: funy() missing 1 required positional argument: 'y'
-------------------------------------------------------------------------
>>> def funx(x):
def funy(y):
return x * y
return funy
>>> i = funx(8)
>>> i(5)
40
>>> i = funx(8)(5)
>>> i
40
本帖最后由 qiuyouzhi 于 2020-2-20 17:08 编辑
因为没有参数啊,你在定义funy的时候定义了一个参数y,你却没有输入
而不加括号有一个优点就是你可以随时调用funy(想用就用,不想用就不用)
但加了括号就一定会调用funy了
|
|