在 Python 中将值从一个函数传递到另一个函数 [英] Passing value from one function to another in Python

查看:76
本文介绍了在 Python 中将值从一个函数传递到另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Python 并且正在努力理解如何在从第一个函数返回值后将多值发送到下一个函数?另外在Python中调用多个函数是否需要使用main()?

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? Also is it necessary to use main() in Python to call more than one functions?

在下面的代码中,我想将 acc_name 和 rg_name 都传递给函数 stop().

In the following code, I would want to pass both acc_name and rg_name to the function 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

    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)

我是 Python 的新手,我的代码可能与概念存在差异.使用 Python 2.7

I am a newbiew to Python, my code might have discrepancies from the concept. Using Python 2.7

任何帮助将不胜感激.提前致谢

Any help would be appreciated. Thanks in advance

推荐答案

我将假设您要问的是如何将多个参数从 functionA 传递到 functionB 而不像您一样将参数存储在两个函数调用之间在片段二中做了.那么如何避免:

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. 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())

或者在你的情况下:

stop(*handle(event, context))

这篇关于在 Python 中将值从一个函数传递到另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆