简单的装饰器

简单的装饰器


def my_decorator(func):
    def wrapper():
        print('wrapper of decorator')
        func()
    return wrapper

def greet():
    print('hello world')

greet = my_decorator(greet)
greet()

这里的函数 my_decorator() 就是一个装饰器,它把真正需要执行的函数 greet() 包裹在其中,

并且改变了它的行为,但是原函数 greet() 不变。

C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled2/socket/fun22.py
wrapper of decorator
hello world



def my_decorator(func):
    def wrapper():
        print('wrapper of decorator')
        func()
    return wrapper

@my_decorator
def greet():
    print('hello world')

greet()

 

你可能感兴趣的:(python,进阶)