如何将未知参数应用于Python 3.x中的函数? [英] How do I apply unknown arguments to a function in Python 3.x?

查看:226
本文介绍了如何将未知参数应用于Python 3.x中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为自己的引擎开发自己的GUI,并且创建了一个对象称为EwConsole(一个简单的下拉式控制台来输入命令)。为了做到这一点,我只需将一个ID作为函数对象本身的关键字存储在一个字典中,该函数被用enter / return触发。我的问题在于参数和Python 3.x。



这是我创建新的控制台命令的方法:

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

我开发了自己的输入对象,叫做EwInput,它有一个方法返回已写入的字符串值。为了分开存储命令的参数,我分割了字符串的空格。下面是在控制台中监视命令的代码:

  def watch_for_commands(self):
if push_enter():
values = self [input]。get_value()。split()$ b $如果self [commands中的值为[0]]:
如果len(值) == 1:
self [commands] [values [0]]()
elif len(values)> 1:
尝试:
self [commands] [values [0]](values [1:])#这里有问题。
除了TypeError:
raise ErgameError(给定的函数'{0}'不支持给定的参数:{1}。.format(values [0],values [1:]))
else:
print(没有这样的命令:{}。format(values [0]))

正如您所看到的,由于命令创建是完全通用的,因此无法知道给定函数将具有多少个参数。 正如人们可以在这里看到的,在旧的2.x中,它是可能使用申请与参数列表。



现在提出的问题:



Python 3.x,如果强迫我使用__ call __,我可以将未知数量的参数应用于某个函数吗?在Python 3.x中有没有办法将一个任意的参数SEQUENCE应用到一个未知函数? 解决方案

尝试使用参数解包

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


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?

解决方案

Try using argument unpacking.

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

这篇关于如何将未知参数应用于Python 3.x中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆