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

I'm developing my own GUI for my engine, and I've created an "object" called EwConsole (A simple "drop-down" console to input commands). In order to do that, I simply store an ID for the function to be "triggered with enter/return" as the key to the function-object itself in a dict. My problem is with the arguments, and Python 3.x.

Here's my method for creating a new "console command":

def create_command(self, command_id, command_function):

self["commands"][command_id] = command_function

I've developed my own input object, called "EwInput", which has a method that returns the string value that has been written in it. In order to separate the arguments of the stored command, I split the spaces of the string. Here's the code that "watches" for "commands" in the console:

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]))

As you can see, since the "command creation" is completely generic, it's not possible to know how many arguments the given function will have. As one can see here, in the old 2.x, it was possible to use "apply" with a list of arguments.

Now comes the question:

How, in Python 3.x, can I "apply" an unknown number of arguments to a function, if it is forcing me to use "__ call __"??? Is there a way in Python 3.x to apply an arbitrary SEQUENCE of arguments to an unknown function?

解决方案self["commands"][values[0]](*values[1:])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值