dynamically adding functions to a Python module

后端 未结 2 636
星月不相逢
星月不相逢 2020-12-19 01:00

Our framework requires wrapping certain functions in some ugly boilerplate code:

def prefix_myname_suffix(obj):
    def actual():
        print \'hello world         


        
相关标签:
2条回答
  • 2020-12-19 01:14

    use either

    current_module.new_name = func
    

    or

    setattr(current_module, new_name, func)
    
    0 讨论(0)
  • 2020-12-19 01:36

    It seems the solution to your problem would be to make the decorated function act as the original function.

    Try using the function mergeFunctionMetadata from Twisted, found here: twisted/python/util.py

    It makes your decorated function act as the original, hopefully making the framework pick it up.

    0 讨论(0)
提交回复
热议问题