【问题标题】:pass the multiple return values from one function to another function as an argument将一个函数的多个返回值作为参数传递给另一个函数
【发布时间】:2021-04-10 09:57:01
【问题描述】:

我有以下代码,

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. 现在,我想使用 hello() 函数的返回值(data & data1) 在 world() 函数中,也想使用 world() 的返回值 另一个函数中的函数。
  2. 此外,world() 函数将作为模块导入,并且应该 在另一个模块的函数中使用。

我该怎么做?

【问题讨论】:

    标签: python-3.x function return python-module


    【解决方案1】:
    1. 您可以使用* 运算符来解压返回值。以下示例适用于我:
    def func1():
        return 10, 's10'
    
    def func2(code, codeString):
        print("code: ", code)
        return codeString
       
    
    cs = func2(*func1())
    print("Code string: ", cs)
    

    更多详情请查看tuples as function arguments

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 2017-08-07
      • 2023-01-12
      • 2013-11-19
      • 1970-01-01
      相关资源
      最近更新 更多