Loading... > 在项目开发中, 总是需要用到装饰器, 但是往往希望通过面向对象的方式来实现wrapper, 使得代码流畅, 更优雅, 这里分享下我是如何实现的: **其实要抓住装饰器@符号的本质: new_same_func = Wrapper(be_decorated_func)** > ```python > # author: Michael > # email: yangowen@126.com > > > class Wrapper: > def __init__(self, func): > self.f = func > > def __call__(self, *args, **kwargs): > print('do something') > res = self.f() > return res > > > > # @Wrapper > def fashuoshuo(): > print("发说说") > > # Note: 通过装饰器本质, 倒推, 用类来实现装饰器 > fashuoshuo = Wrapper(fashuoshuo) > > fashuoshuo() > ``` © 允许规范转载