Decorator

1. Callable

Any object which implements the method __call__() is termed callable.

2. Decorator

  • metaprogramming

  • inside a decorator, define a func and return it.

def star(func):
    def inner(*args, **kwargs):
        print("*" * 30)
        func(*args, **kwargs)
        print("*" * 30)
    return inner

def percent(func):
    def inner(*args, **kwargs):
        print("%" * 30)
        func(*args, **kwargs)
        print("%" * 30)
    return inner

@star
@percent
def printer(msg):
    print(msg)

printer("Hello")

其中

3. Another example

Last updated

Was this helpful?