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

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

问题描述

如果已经提出并回答了这个问题,我们深表歉意.我需要做的在概念上非常简单,但遗憾的是我一直无法在网上找到答案.

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

我从一个名字列表开始.

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

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

我希望结果是:

 def func1(*args):...def func2(*args):...def func3(*args):...

我需要这样做的原因是每个函数名对应一个测试用例,然后从外部调用.

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

更新:我也可以将这个自定义命名函数包装在一个类中,如果这会让事情变得更容易.

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

例如:

 func_content = """对于 args 中的 arg:打印参数"""

提前致谢,

马赫迪

解决方案

对于你所描述的,我认为你不需要深入研究 eval 或宏——通过闭包创建函数实例应该可以正常工作.示例:

def bindFunction1(name):def func1(*args):对于 args 中的 arg:打印参数返回 42 # ...func1.__name__ = 名称返回函数1def bindFunction2(name):def func2(*args):对于 args 中的 arg:打印参数返回 2142 # ...func2.__name__ = 名称返回函数2

但是,您可能希望按名称将这些函数添加到某个范围,以便您可以按名称访问它们.

<预><代码>>>>打印 bindFunction1('整洁')<函数在 0x00000000629099E8 处整齐>>>>打印 bindFunction2('敏锐')<功能敏锐于 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天全站免登陆