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 <module>
    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 <module>
    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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫猫虫(——)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值