通过第三方包wrapt简单的修改下:
import wrapt def tag(name="div",title=None): @wrapt.decorator def _tag(wrapped, instance, args, kwargs): result = None if title is None: result = f"<{name}>"+wrapped(*args,**kwargs)+f"</{name}>" else: result = f"<{name} class='{title}'>"+wrapped(*args,**kwargs)+f"</{name}>" return result return _tag @tag(name="h1",title="say") def foo(msg): return "hello world" @tag() def hello(): return "hehe" print(foo("abc")) print(hello())转载于:https://www.cnblogs.com/c-x-a/p/11129560.html