簡體   English   中英

將多個返回值從一個 function 傳遞給另一個 function 作為參數

[英]pass the multiple return values from one function to another function as an argument

我有下面的代碼,

def hello(start, end):
    perform some function
    for i in range(j):
        yield (some value)
    yield (some value)

def calc(start, end):
    start = "x"
    end = "y"
    for d in hello(start, end):
        perform some function       
        data = 123
        perform some function 
        data1 = 456
    return data, data1

def world(hello()):
    with open(r"C:\filepath\abc.json",'w') as file:
        json.dump(hello.data, file)
    
    with open(r"C:\filepath\abc.json") as file1:
        d = json.load(file1)
    return d
  1. Now, I want to use return values(data & data1) from hello() function in world() function, also want to use return values from world() function in another function.
  2. 此外,world() function 將作為一個模塊導入,並且應該在另一個模塊的 function 中使用。

我怎么能那樣做?

  1. 您可以使用*運算符解壓縮返回值。 以下示例適用於我:
def func1():
    return 10, 's10'

def func2(code, codeString):
    print("code: ", code)
    return codeString
   

cs = func2(*func1())
print("Code string: ", cs)

有關詳細信息,請參閱元組 function arguments

暫無
暫無

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

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