簡體   English   中英

Python:函數參數和返回值

[英]Python: function arguments and return values

說我有一個功能

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)

您需要從函數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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM