使用自定义名称创建Python动态函数 [英] Python dynamic function creation with custom names

查看:278
本文介绍了使用自定义名称创建Python动态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果此问题已提出并回答,请表示歉意。
我需要做的是在概念上很简单,但很遗憾,我没有能够在网上找到一个答案。



我需要创建动态函数在Python(Python2.7)与运行时的自定义名称。每个函数的主体也需要在运行时构建,但对于所有函数它(几乎)是相同的。



我从一个名字列表开始。 p>

  func_names = [func1,func2,func3] 
pre>

请注意,func_name列表可以包含任意名称的列表,因此名称不会简单地是func1,func2,func3,....



我想要的结果是:

  def func1(* args):
...

def func2(* args):
...

def func3(* args):
...

我需要这样做的原因是每个函数名称对应一个测试用例,



更新:没有用户输入。我绑在一个更大的模块的两端。一端确定测试用例是什么,以及其他内容,填充测试用例名称的列表。另一端是函数本身,它必须与测试用例的名称具有1:1映射。所以我有测试用例的名称,我知道我想对每个测试用例做什么,我只需要创建具有测试用例名称的函数。由于测试用例的名称在运行时确定,因此基于这些测试用例的函数创建也必须在运行时。



更新 :我也可以将这个自定义命名函数包装在类中,如果这将使事情更容易。



我可以硬编码的函数的内容相同)在字符串中,或​​者我可以基于它先前定义的基类。只需要知道如何使用此内容填充函数。



例如:

 code> func_content =
在args中的arg:
打印arg

提前感谢



Mahdi

解决方案

对于你所描述的,我不认为你需要下降到eval或宏 - 通过闭包创建函数实例应该工作很好。示例:

  def bindFunction1(name):
def func1(* args):
args:
print arg
return 42#...
func1 .__ name__ = name
return func1

def bindFunction2(name):
def func2(* args):
在args中的arg:
print arg
return 2142#...
func2 .__ name__ = name
return func2

但是,您可能希望通过名称将这些函数添加到某个作用域,以便您可以按名称。

 >>> print bindFunction1('neat')
< function neat at 0x00000000629099E8>
>>>> print bindFunction2('keen')
< function keen at 0x0000000072C93DD8>


Apologies if this question has already been raised and answered. What I need to do is very simple in concept, but unfortunately I have not been able to find an answer for it online.

I need to create dynamic functions in Python (Python2.7) with custom names at runtime. The body of each function also needs to be constructed at runtime but it is (almost) the same for all functions.

I start off with a list of names.

func_names = ["func1", "func2", "func3"]

Note that the func_name list can hold a list of arbitrary names, so the names will NOT simply be func1, func2, func3, ....

I want the outcome to be :

    def func1(*args):
        ...

    def func2(*args):
        ...

    def func3(*args):
        ...

The reason I need to do this is that each function name corresponds to a test case which is then called from the outside world.

update: There is no user input. I'm tying two ends of a much bigger module. One end determines what the test cases are and among other things, populates a list of the test cases' names. The other end is the functions themselves, which must have 1:1 mapping with the name of the test case. So I have the name of the test cases, I know what I want to do with each test case, I just need to create the functions that have the name of the test cases. Since the name of the test cases are determined at runtime, the function creation based on those test cases must be at runtime as well.

update: I can also wrap this custom named functions in a class if that would make things easier.

I can hard-code the content of the functions (since they are almost the same) in a string, or I can base it off of a base class previously defined. Just need to know how to populate the functions with this content.

For example:

    func_content = """
                   for arg in args:
                       print arg
                   """

Thanks in advance,

Mahdi

解决方案

For what you describe, I don't think you need to descend into eval or macros — creating function instances by closure should work just fine. Example:

def bindFunction1(name):
    def func1(*args):
        for arg in args:
            print arg
        return 42 # ...
    func1.__name__ = name
    return func1

def bindFunction2(name):
    def func2(*args):
        for arg in args:
            print arg
        return 2142 # ...
    func2.__name__ = name
    return func2

However, you will likely want to add those functions by name to some scope so that you can access them by name.

>>> print bindFunction1('neat')
<function neat at 0x00000000629099E8>
>>> print bindFunction2('keen')
<function keen at 0x0000000072C93DD8>

这篇关于使用自定义名称创建Python动态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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