Python:函数参数和返回值

时间:2012-12-07 09:04:08

标签: python function

说我有一个功能

def equals_to(x,y):
 a + b = c
def some_function(something):
 for i in something:
 ...

有没有办法使用c计算的equals_to作为此some_function的参数

equals_to(1,2)
some_function(c)

1 个答案:

答案 0 :(得分:3)

您需要return函数中c的值。

def equals_to(x,y):
    c = x + y           # c = x + y not a + b = c
    return c            # return the value of c 

def some_function(something):
    for i in something:
    ... 
    return 

sum = equals_to(1,2)     # set sum to the return value from the function 
some_function(sum)       # pass sum to some_function

equals_to的函数签名也会使用参数x,y,但在您使用的函数a,b中,您的作业轮回错误,c取值{ x + y而非a + b等于c

强烈建议:http://docs.python.org/2/tutorial/