How to Make Decorators Optionally Turn On Or Off

拈花ヽ惹草 提交于 2019-11-30 16:43:34

Attach the undecorated function to the decorated one, as unwrapped say, before returning it from the decorator.

For example

def add42(fn):
    def wrap(i):
        return fn(i) + 42
    wrap.unwrapped = fn
    return wrap

@add42
def mult3(i):
    return i * 3

mult3(1) # 45
mult3.unwrapped(1) # 3
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!