如何将未知参数应用于 Python 3.x 中的函数?

How do I apply unknown arguments to a function in Python 3.x?

我正在为我的 engine 开发自己的 GUI,并且我创建了一个名为 EwConsole 的 "object"(一个简单的 "drop-down" 控制台来输入命令)。为了做到这一点,我只是将函数的 ID 存储为 "triggered with enter/return" 作为字典中函数对象本身的键。我的问题是参数,Python 3.x.

这是我创建新 "console command" 的方法:

def create_command(self, command_id, command_function):
    self["commands"][command_id] = command_function

我开发了自己的输入对象,称为 "EwInput",它有一个方法可以 returns 写入其中的字符串值。为了分隔存储命令的参数,我拆分了字符串的空格。这是控制台中 "watches" for "commands" 的代码:

def watch_for_commands(self):
    if push_enter():
        values = self["input"].get_value().split()
        if values[0] in self["commands"]:
            if len(values) == 1:
                self["commands"][values[0]]()
            elif len(values) > 1:
                try:
                    self["commands"][values[0]](values[1:]) # Problem here.
                except TypeError:
                    raise ErgameError("The given function '{0}' does not support the given arguments: {1}.".format(values[0], values[1:]))
        else:
            print("There is no such command: {}".format(values[0]))

如您所见,由于 "command creation" 是完全通用的,因此无法知道给定函数将有多少个参数。 As one can see here,在旧的 2.x 中,可以使用带有参数列表的 "apply"。

问题来了:

在 Python 3.x 中,我如何 "apply" 一个函数的未知数量的参数,如果它迫使我使用“__ call __”??? Python 3.x 中有没有办法将任意参数序列应用于未知函数?

尝试使用 argument unpacking

self["commands"][values[0]](*values[1:])