True dynamic and anonymous functions possible in Python?

前端 未结 7 1464
说谎
说谎 2020-11-28 05:34

Just as a dynamic class can be created using type(name, base-classes, namespace-dict), can a dynamic function be created?

I\'ve tried doing something along the line

7条回答
  •  心在旅途
    2020-11-28 05:56

    There is types.FunctionType which you can use to dynamically create a function e.g.

    def test_func(): print 'wow' 
    dynf = types.FunctionType(test_func.func_code, {})
    dynf()
    

    Output:

    wow
    

    You might object that this is not dynamic because I am using code from another function, but that was just an example there is a way to generate code from python strings e.g.

    dynf = types.FunctionType(compile('print "really WoW"', 'dyn.py', 'exec'), {})
    dynf()
    

    Output:

    really WoW
    

    Now that is dynamic!

    OP is worried about the dynamic nature of such function so here is another example

    dynf = types.FunctionType(compile('test_func():\ntest_func()', 'dyn.py', 'exec'), globals())
    dynf()
    

    Output:

    wow
    wow
    

    Note: Creating Function object like this seems to have limitations e.g. it is not easy to pass arguments, because to pass arguments we need to pass correct co_argcount, co_varnames and other 12 variables to types.CodeType, which theoretically can be done but will be error prone, an easier way is to import string as a module and you have a full fledged function e.g.

    import types
    import sys,imp
    
    code = """def f(a,b,c):
        print a+b+c, "really WoW"
    """
    module = imp.new_module('myfunctions')
    exec code in module.__dict__
    module.f('W', 'o', 'W')
    

    Output:

    WoW really WoW
    

提交回复
热议问题