Python 3.X 对不同的东西使用多个 *args

Python 3.X using multiple *args for different stuff

我知道当您不知道在函数调用中使用了多少 args 时会使用 *args。但是,当我想要一组 *args 做 X 而一组 *args 做 y 时,我该怎么办?

在这种情况下,您将参数作为两个单独的列表,它们可以默认为空列表或 None。

您不能在单个函数中传递两个 *args。您需要将 args1args2 作为普通 list 传递,您可以将这些 lists 作为 args 传递执行 XY 的函数。例如:

def do_X(*args):
    # Do something

def do_Y(*args):
    # Do some more thing

def my_function(list1, list2):
    do_X(*list1)  # Pass list as `*args` here
    do_Y(*list2)

你对 my_function 的调用将是这样的:

args_1 = ['x1', 'x2']  # Group of arguments for doing `X`
args_2 = ['y1', 'y2']  # Group of arguments for doing `Y`

# Call your function
my_function(args_1, args_2)