【发布时间】:2012-01-12 23:34:37
【问题描述】:
我有一个 hello 函数,它需要 n 个参数(见下面的代码)。
def hello(*args):
# return values
我想从*args 返回多个值。怎么做?例如:
d, e, f = hello(a, b, c)
解决方案:
def hello(*args):
values = {} # values
rst = [] # result
for arg in args:
rst.append(values[arg])
return rst
a, b, c = hello('d', 'e', f)
a, b = hello('d', 'f')
只是返回列表。 :) :D
【问题讨论】:
-
不知道你的意思。
return args将从*args返回多个值。 -
重复,这里详细回答。 [堆栈溢出 ret mul val python][1] [1]: stackoverflow.com/questions/354883/…
-
在 hello 函数中,我正在为变量赋值。我从 *args 中得到这些变量。你知道 args 是动态的。所以我不知道从动态变量 /args/ 中返回多个值,例如示例。
标签: python function python-3.x python-2.x