Python函数装饰器

一:Python可将函数作为变量赋值

创建一个函数,将函数赋值给一个变量,如下:

def test1():
    print("hello world!")

if __name__ == "__main__":
    a = test1
    a()
    test1()

 test1是一个简单的打印hello world!的函数,将函数test1赋值给变量a,调用a()可以看到和test1是一致的:

PS C:\Users\...> & D:/anaconda/python.exe c:/Users/.../Desktop/WorkSpace/函数装饰器.py
hello world!
hello world!

在赋值后使用del函数删除test1,再调用a,可以看到在赋值后对原函数的改变不会影响被赋值的变量:

a = test1
del(test1)
test1()
PS C:\Users\...> & D:/anaconda/python.exe c:/Users/.../Desktop/WorkSpace/函数装饰器.py
Traceback (most recent call last):
  File "c:/Users/.../Desktop/WorkSpace/函数装饰器.py", line 7, in 
    test1()
NameError: name 'test1' is not defined

 

a = test1
del(test1)
a()
PS C:\Users\...> & D:/anaconda/python.exe c:/Users/.../Desktop/WorkSpace/函数装饰器.py
hello world!

 

二:Python可将函数作为函数的返回值

 先看下面的代码示例:

def test1():
    print("hello world!")

def test2():
    return test1

if __name__ == "__main__":
    a = test2()
    a()
PS C:\Users\...> & D:/anaconda/python.exe c:/Users/.../Desktop/WorkSpace/函数装饰器.py
hello world!

可以看到调用a(),依然输出hello world!

同理,也可以将函数作为函数的参数,传递到函数内部:

def test1():
    print("hello world!")

def test2(func):
    func()

if __name__ == "__main__":
    test2(test1)
PS C:\Users\...> & D:/anaconda/python.exe c:/Users/.../Desktop/WorkSpace/函数装饰器.py
hello world!

 

三:Python可以在函数中定义函数 

函数内部定义的函数,在函数外部不能直接调用,只能在函数内部调用,或者作为返回值传递:

def test1():
    def test2():
        print("hello world!")
    test2()
    return test2

if __name__ == "__main__":
    a = test1()
    a()
PS C:\Users\...> & D:/anaconda/python.exe c:/Users/.../Desktop/WorkSpace/函数装饰器.py
hello world!
hello world!

 

def test1():
    def test2():
        print("hello world!")
    return test2

if __name__ == "__main__":
    test2()
PS C:\Users\...> & D:/anaconda/python.exe c:/Users/.../Desktop/WorkSpace/函数装饰器.py
Traceback (most recent call last):
  File "c:/Users/.../Desktop/WorkSpace/函数装饰器.py", line 7, in 
    test2()
NameError: name 'test2' is not defined

 

四:Python函数装饰器 

在上述三种功能的支持下,有如下一个需求:

例如,我们编写了一个函数,打印hello world!:

def test1():
    print("hello world!")

现在,项目经理告诉你,函数要加一个功能,打印出“Python is the best programming language” ,我们会在函数中直接添加:

def test1():
    print("hello world!")
    print("Python is the best programming language")

但是项目经理又告诉你,不要改变原函数,这怎么办?看下面示例:

def test1():
    print("hello world!")

def test2(func):
    def new_test1():
        func()
        print("Python is the best programming language")
    return new_test1

if __name__ == "__main__":
    a = test2(test1)
    a()
PS C:\Users\...> & D:/anaconda/python.exe c:/Users/.../Desktop/WorkSpace/函数装饰器.py
hello world!
Python is the best programming language

test2实现了,在任意函数func后打印 “Python is the best programming language”,这就是一个装饰器,但是在使用时,用@,如下:

def test2(func):
    def new_test1():
        func()
        print("Python is the best programming language")
    return new_test1

@test2
def test1():
    print("hello world!")

if __name__ == "__main__":
    test1()
PS C:\Users\...> & D:/anaconda/python.exe c:/Users/.../Desktop/WorkSpace/函数装饰器.py
hello world!
Python is the best programming language

@符号加装饰器函数名,放在要添加装饰器的函数定义的上方,在不改变函数的情况下重写函数功能。 

函数装饰器的功能强大,上面所举的例子并不是要说明函数装饰器的应用场景,只是说明原理,其应用场景不是本文的内容,有兴趣的可以自行查找资料。

你可能感兴趣的:(Python语言)