Python : 装饰器Decorators

装饰器 Decorators 用来给对象动态添加一些新的行为.闭包也是这样.
>>> def my_decorator(func):
        def wrapper(*args, **kwargs):
            print("Before call")
            result = func(*args, **kwargs)
            print("After call")
            return result
        return wrapper

>>> @my_decorator
    def add(a, b):
        return a + b

>>> add(1, 3)
Before call
After call
4

你可能感兴趣的:(Python : 装饰器Decorators)