简体   繁体   English

在Python中将值从一个函数传递给另一个函数

[英]Passing value from one function to another in Python

I am learning Python and struggling to understand on how to send multi-values to the next function after returning the value from the first function? 我正在学习Python,并努力了解如何在从第一个函数返回值之后将多值发送到下一个函数? Also is it necessary to use main() in Python to call more than one functions? 还有必要在Python中使用main()来调用多个函数吗?

In the following code, I would want to pass both acc_name and rg_name to the function stop(). 在下面的代码中,我想将acc_name和rg_name都传递给函数stop()。

I have this following simplified code, where the logic works as expected, hence have not included that as I just want to understand the workflow of the code. 我有以下简化的代码,其中的逻辑按预期工作,因此未包含该代码,因为我只是想了解代码的工作流程。

    def handle(event, context):
       #code logic
       return acc_name, rg_name
    def stop(acc_name, rg_name):
        #code logic
    return sg_id

OR 要么

    def handle(event, context):
       #code logic
       return acc_name, rg_name
    def stop(x,y):
        #code logic
    return sg_id

    def main():
      x,y = handle(event, context)
      stop(x,y)

I am a newbiew to Python, my code might have discrepancies from the concept. 我是Python的新手,我的代码可能与概念不符。 Using Python 2.7 使用Python 2.7

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance 提前致谢

I'm going to assume that what you are asking is how to pass multiple arguments from functionA to functionB without storing the arguments in between the two function calls like you did in snippet two. 我将假设您要问的是如何将多个参数从functionA传递到functionB,而又不像在摘要2中那样将参数存储在两个函数调用之间。 So how to avoid: 那么如何避免:

x, y = functionA()
functionB(x, y)

This you can do by unpacking the arguments here's the docs for the feature . 您可以通过解压缩功能文档的参数来做到这一点。 You do this by calling: 为此,您可以致电:

functionB(*functionA())

or in your case: 或者您的情况:

stop(*handle(event, context))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM