一种选择是更改原型以将可迭代作为multi_values
参数,而不是接受任意数量的参数:
def example(var1=None, var2=None, var3=None, multi_values=None):
if multi_values is None:
multi_values = []
# whatever
然后你可以调用函数
example(multi_values=multi)
另一种选择是仅接受作为var1
关键字var3
参数:
def example(*multi_values, **kwargs):
var1 = kwargs.pop("var1", None)
var2 = kwargs.pop("var2", None)
var3 = kwargs.pop("var3", None)
if kwargs:
raise TypeError("Unknown keyword arguments")
# whatever
在 Python 3.x 中,通过在参数后var1
添加through来支持这一点:var3
*multi_values
def example(*multi_values, var1=None, var2=None, var3=None):
# whatever
This does basically the same as the previous example, with the additional advantage of better introspection.