這是不可能的,因爲*arg
從該位置捕獲所有位置參數。所以根據定義,第二個*args2
將永遠是空的。
一個簡單的解決辦法是通過兩個元:
def myfunction(type, id, args1, args2):
other_function(args1)
other_function2(args2)
,並調用它像這樣:
myfunction(type, id, (1,2,3), (4,5,6))
如果這兩個函數期望位置參數,而不是一個單獨的參數,你會打電話他們是這樣的:
def myfunction(type, id, args1, args2):
other_function(*arg1)
other_function2(*arg2)
這樣做的好處是可以使用任何在調用myfunction
時可迭代,甚至是一個生成器,因爲被調用的函數永遠不會與傳入的迭代進行接觸。
如果你真的想使用兩個可變參數列表,你需要某種分隔符。下面的代碼使用None
作爲分隔符:
import itertools
def myfunction(type, id, *args):
args = iter(args)
args1 = itertools.takeuntil(lambda x: x is not None, args)
args2 = itertools.dropwhile(lambda x: x is None, args)
other_function(args1)
other_function2(args2)
它會像這樣使用:
myfunction(type, id, 1,2,3, None, 4,5,6)
你可以舉一個例子來說明你希望如何調用這個函數嗎? –
Python如何知道參數是否應該在'arg1'或'arg2'中,如果兩者都是可變的? –