我想在python中添加一个新功能,纯粹是为了实验目的,我想扩展装饰器语法。目前,装饰器可以应用于函数和类。我还想在循环(例如for循环)和代码块上使用装饰器。# there's some code now # and something more
现在,虽然这是基本的想法,但我的要求是修改装饰器</e
我正在尝试实现一个装饰器,它接收一个函数,将其解析为AST,最终会对AST做一些事情,然后从AST中重构原始(或修改)函数并返回它。我目前的方法是,一旦我有了AST,将其compile为一个code <module>对象,然后在其中获取带有函数名称的常量,将其转换为FunctionType,并返回它。def test
= ast.parse(src) code_obj = compile(new_ast, '<auto-generatedfn.generated_fn = myscope['name'] # Where name is the binding created by execing code_obj
综上所述,mylib.codegen是一个装饰<