首页 > 代码库 > Python 3.x--装饰器
Python 3.x--装饰器
————————装饰器=高阶函数+嵌套函数——————————
高阶函数:1、把一个函数名当做实参传递给另一个函数;2、返回值中包含函数名
def func1():
print("this is func1")
def test(func):
print(func)
func()
test(func1)
输出结果:
装饰器:为其他函数添加附加功能,不改变原函数代码及调用方式
import time
def bar():
time.sleep(2)
print("this is bar")
def test1(func1):
print(func1)
return func1
bar = test1(bar)
bar()
不改变调用方式,未添加新功能---------
import time
def deco(func):
start_time = time.time()
return func
stop_time = time.time()
print(‘Running time is %s:‘%(stop_time-start_time))
def test1():
time.sleep(2)
print("this is test1")
def test2():
time.sleep(2)
print("this is test2")
test1 = deco(test1)
test1()
test2 = deco(test2)
test2()
运行结果:
不改变调用方式,添加新功能---------
import time
def timer(func):
def deco():
start_time = time.time()
func()
stop_time = time.time()
print(‘Running time is %s:‘%(stop_time-start_time))
return deco
def test1():
time.sleep(2)
print("this is test1")
def test2():
time.sleep(3)
print("this is test2")
test1 = timer(test1)
test1()
test2 = timer(test2)
test2()
运行结果:
使用@符号-----------
import time
def timer(func):
def deco():
start_time = time.time()
func()
stop_time = time.time()
print(‘Running time is %s:‘%(stop_time-start_time))
return deco
@timer
def test1():
time.sleep(2)
print("this is test1")
def test2():
time.sleep(3)
print("this is test2")
test1()
test2 = timer(test2)
test2()
原函数带有参数-----------
import time
def timer(func):
def deco(*arg,**kwarg):
start_time = time.time()
func(*arg,**kwarg)
stop_time = time.time()
print(‘Running time is %s:‘%(stop_time-start_time))
return deco
@timer
def test1():
time.sleep(2)
print("this is test1")
@timer #test2 = timer(test2)
def test2(name,age):
time.sleep(1)
print("this is test2:",name,age)
test1()
test2(‘lili‘,18)
运行结果:
原函数有返回值----------
user,passwd = ‘lili‘,‘abc‘
def auth(func):
def wrapper(*args,**kwargs):
username = input("Username:").strip()
password = input("Passwd:").strip()
if user == username and passwd ==password:
print("\033[32;1mUser has passed authenticatiom\033[0m")
return func(*args,**kwargs)
else:
exit("\033[31;1mInvalid username or password\033[0m")
return wrapper
@auth
def page1():
print("this is page1")
return "from page1"
@auth
def page2():
print("this is page2")
@auth
def page3():
print("this is page3")
print(page1())
运行结果:
装饰器带参数-------------
user,passwd = ‘lili‘,‘abc‘
def auth(auth_type):
print("auth_arge:",auth_type)
def outer_wrapper(func):
def wrapper(*args,**kwargs):
if auth_type == ‘p1‘:
username = input("Username:").strip()
password = input("Passwd:").strip()
if user == username and passwd ==password:
print("\033[32;1mUser has passed authenticatiom\033[0m")
return func(*args,**kwargs)
else:
exit("\033[31;1mInvalid username or password\033[0m")
elif auth_type == ‘p2‘:
print(‘p2 pass‘)
return wrapper
return outer_wrapper
@auth(auth_type = "p1")
def page1():
print("this is page1")
return "from page1"
@auth(auth_type = "p2")
def page2():
print("this is page2")
print(page1())
page2()
运行结果:
Python 3.x--装饰器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。