将函数调用作为函数参数传递

Abh*_*tia 1 python python-2.7 python-3.x

码:

def function1(a,b):
    return a-1,b-1
def function2(c,d):
    return c+1,d+1

print function1(function2(1,2))   
Run Code Online (Sandbox Code Playgroud)

错误:

Traceback (most recent call last):
  File "C:\Users\sony\Desktop\Python\scripts\twitter_get_data.py", line 6, in <module>
    print function1(function2(1,2))    
TypeError: function1() takes exactly 2 arguments (1 given)
[Finished in 0.1s with exit code 1] 
Run Code Online (Sandbox Code Playgroud)

为什么出现上述错误?

Bri*_*ien 5

函数返回元组,因为return只返回一个项目.您可以通过在星号前加上"解包"返回的元组.语法如下所示:

print function1(*function2(1,2))   
Run Code Online (Sandbox Code Playgroud)