day④:装饰器
start
py3
一.装饰器基本原理与实现
例子1:
#!/usr/bin/python
#coding=utf-8
#py3
def login(func): #func=tv
print("passed user verification...")
return func #返回func,即是tv的内存地址
def tv():
print("welcome [%s] to home page")
tv = login(tv) #tv是变量
tv() #tv加上括号,就调用tv函数
结果:
passed user verification...
welcome [%s] to home page
例子1.1 ,加上参数和用语法糖:
#!/usr/bin/python
#coding=utf-8
#py3
def login(func): #func=tv
print("passed user verification...")
return func #返回func,即是tv的内存地址
@login #tv=login(tv)
def tv(name):
print("welcome [%s] to home page" %name)
tv("yaobin") #tv加上括号,就调用tv函数
结果:
passed user verification...
welcome [yaobin] to home page
例子1.2,小问题,还没调用就执行验证了:
#!/usr/bin/python
#coding=utf-8
#py3
def login(func): #func=tv
print("passed user verification...")
return func #返回func,即是tv的内存地址
@login #tv=login(tv)
def tv(name):
print("welcome [%s] to home page" %name)
结果:
passed user verification...
#我都还没有tv("yaobin"),就已经执行了验证
二.装饰器实现
为了解决上面的问题,加上inner():
例子1:
#!/usr/bin/python
#coding=utf-8
#py3
def login(func): #func=tv
def inner(arg):
print("passed user verification...")
#return func #这里不用return了,应该是执行!
func(arg) #tv
return inner #login 返回inner的内存地址
@login #tv=login(tv)
def tv(name):
print("welcome [%s] to home page" %name)
#tv("yaobin") #这里加上括号,相当于调用inner!而且有参数,所有inner也要有参数,func也要有参数
①结果:
空
②把#tv("yaobin")打开,结果:
passed user verification...
welcome [yaobin] to home page
#程序一执行,就执行@login,还没调用就执行了,所以加上inner().返回inner的内存地址,解决还没调用就执行@login。
三.被装饰的函数如果有参数:
(1).2个参数
#!/usr/bin/python
#coding=utf-8
#py3
def login(func): #func=tv or moive
def inner(arg1,arg2):
print("passed user verification...")
#return func #这里不用return了,应该是执行!
func(arg1,arg2) #tv or moive
return inner #login 返回inner的内存地址
@login
def moive(name,anything): #moive=moive(tv)
print("welcome [%s] to home page" %name)
@login #tv=login(tv)
def tv(name,passwd):
print("welcome [%s] to home page" %name)
print("my passwd is :[%s] " %passwd)
tv("yaobin","123456")
print("")
moive("hy","anything")
#结果
passed user verification...
welcome [yaobin] to home page
my passwd is :[123456]
passed user verification...
welcome [hy] to home page
(2).装饰具有处理n个参数的函数的装饰器
#!/usr/bin/python
#coding=utf-8
#py3
def login(func): #func=tv
def inner(*args,**kwargs):
print("passed user verification...")
#return func #这里不用return了,应该是执行!
func(*args,**kwargs) #tv or moive
return inner #login 返回inner的内存地址
@login #moive=moive(tv)
def moive(name,passwd=123456): #passwd有默认参数
print("welcome [%s] to home page" %name)
print("my passwd is :[%s] " %passwd)
@login #tv=login(tv)
def tv(name,passwd=456789): #passwd有默认参数
print("welcome [%s] to home page" %name)
print("my passwd is :[%s] " %passwd)
#①自定义参数
moive("hy","one")
tv("yaobin","two")
print("")
#②使用默认参数
moive("hy")
tv("yaobin")
结果:
passed user verification...
welcome [hy] to home page
my passwd is :[one]
passed user verification...
welcome [yaobin] to home page
my passwd is :[two]
passed user verification...
welcome [hy] to home page
my passwd is :[123456]
passed user verification...
welcome [yaobin] to home page
my passwd is :[456789]
四.被装饰的函数返回值
#!/usr/bin/python
#coding=utf-8
#py3
def login(func): #func=tv or moive
def inner(*args,**kwargs):
print("passed user verification...")
#return func #这里不用return了,应该是执行!
return func(*args,**kwargs) #tv or moive ##这里要return,不然没有返回值
return inner #login 返回inner的内存地址
@login #moive=moive(tv)
def moive(name,passwd=123456): #passwd有默认参数
print("welcome [%s] to home page" %name)
print("my passwd is :[%s] " %passwd)
@login #tv=login(tv)
def tv(name,passwd=456789): #passwd有默认参数
print("welcome [%s] to home page" %name)
print("my passwd is :[%s] " %passwd)
return name,passwd
moive("hy") #moive()加上括号,相当于调用inner!而且有参数,所有inner也要有参数,func也要有参数
print("")
t= tv("yaobin","my passwd haha") #tv()加上括号,相当于调用inner!而且有参数,所有inner也要有参数,func也要有参数
print(t) #看返回值
结果:
passed user verification...
welcome [hy] to home page
my passwd is :[123456]
passed user verification...
welcome [yaobin] to home page
my passwd is :[my passwd haha]
('yaobin', 'my passwd haha')
end