dynamically adding functions to a Python module

后端 未结 2 644
星月不相逢
星月不相逢 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)
    

提交回复
热议问题