Python Decorator

Python Decorator

Ref: Python 装饰器为什么难理解?https://zhuanlan.zhihu.com/p/27449649

Background

A simple example

def wrap(aFunc):
    def wrapped():
        print("Starting ...")
        aFunc()  # closure
        print("End")
    return wrapped

def foo():
    print("foo")

foo = wrap(foo)  # wrap / decorate 
foo()


# Using decorator
@wrap
def foo():
    print("foo")

foo()

This is decorator

[DONE]

Table of Contents