从变量设置 *args?
Set *args from variable?
是否可以从变量设置 *args
?
def fn(x, *args):
# ...
# pass arguments not as list but each as single argument
arguments = ??? # i.e.: ['a', 'b']
fn(1, arguments)
# should be equivalent to
fn(1, 'a', 'b')
是的,您可以使用参数解包(也称为展开):
fn(1, *arguments)
下面是演示:
>>> def fn(x, *args):
... return args
...
>>> arguments = ['a', 'b']
>>> fn(1, *arguments)
('a', 'b')
>>>
# pass arguments not as list but each as single argument
arguments = ??? # i.e.: ['a', 'b']
然后应分配参数 arguments = ['a' ,'b']
。这是参数拆包。
是否可以从变量设置 *args
?
def fn(x, *args):
# ...
# pass arguments not as list but each as single argument
arguments = ??? # i.e.: ['a', 'b']
fn(1, arguments)
# should be equivalent to
fn(1, 'a', 'b')
是的,您可以使用参数解包(也称为展开):
fn(1, *arguments)
下面是演示:
>>> def fn(x, *args):
... return args
...
>>> arguments = ['a', 'b']
>>> fn(1, *arguments)
('a', 'b')
>>>
# pass arguments not as list but each as single argument
arguments = ??? # i.e.: ['a', 'b']
然后应分配参数 arguments = ['a' ,'b']
。这是参数拆包。