boo*_*oop 0 python arguments function python-2.7
是否可以*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')
Run Code Online (Sandbox Code Playgroud)
是的,你可以使用参数解包(也称为splatting):
fn(1, *arguments)
Run Code Online (Sandbox Code Playgroud)
以下是演示:
>>> def fn(x, *args):
... return args
...
>>> arguments = ['a', 'b']
>>> fn(1, *arguments)
('a', 'b')
>>>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
358 次 |
| 最近记录: |