Skip to content

decorator

我们很少创建自己的 decorator,而仅在使用别人写好的代码时经常用到它。

当使用 Django 或者 Flask 时,会经常用到 decorator。

Decorators are similar to attributes in that they add meaning or functionality to blocks of code in Python. They're frequently used in frameworks such as Flask or Django. The most common interaction you'll have with decorators as a Python developer is through using them rather than creating them.

decorator 用法示例:

def logger(func):
    def wrapper():
        print('Logging execution')
        func()
        print('Done logging')
    return wrapper

@logger
def sample():
    print('-- Inside sample function')

sample()