【问题标题】:python add a list of variables as positional arguments for functionspython 添加一个变量列表作为函数的位置参数
【发布时间】:2023-02-06 20:58:45
【问题描述】:

我有一个变量列表,需要在几个具有相同数量参数的函数中传递。

a = 1
b = 'hello'
c = 1.9

args = (a, b, c)

op = func_a(args) if <cond1> else func_b(args)

def func_a(a, b, c):
    ...
    
def func_b(a, b, c):
    ...

但是将其作为元组发送,元组设置为 arg a 并且该函数也需要 args bc

如何将这些元组分别作为abc传递?

【问题讨论】:

  • func_a(*args)

标签: python django function arguments


【解决方案1】:

使用* 解压元组:

op = func_a(*args) if <cond1> else func_b(*args)

这将解压元组并将参数作为单独的参数传递。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2020-12-03
    • 1970-01-01
    • 2018-04-15
    相关资源
    最近更新 更多