簡體   English   中英

從變量設置* 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')

是的,您可以使用參數解壓縮(也稱為splatting):

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']分配arguments = ['a' ,'b'] 這是參數解壓縮。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM