|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- >>> def test1(x):
- def test2(a):
- return a
- print(x)
- return x
- >>> test1(1)(2)
- 1
- Traceback (most recent call last):
- File "<pyshell#1222>", line 1, in <module>
- test1(1)(2)
- TypeError: 'int' object is not callable
- >>> def test(x):
- return x
- >>> test(1)
- 1
复制代码
正如楼上所说,你应该返回一个函数体,才能 test1(1)(2) 这样两个括号的调用
否则因为你调用 test1(1) 的返回值是 x,即此时 x 为 test1 传入的 1 ,则返回结果 1 后你对这个值进行调用
即你调用 test1(1)(2) 相当于这样调用 1(2) ,而 1 为 int 型,肯定无法进行调用而导致报错
所以你将 return 那更为内嵌函数即可,参考代码:
- def test1(x):
- def test2(a):
- return a
- print(x)
- return test2
- print(test1(1)(2))
复制代码
你代码更改成这样,调用 test1(1)(2) 就相当于调用 test2(2),即可成功调用
|
|