码迷,mamicode.com
首页 > 编程语言 > 详细

Python 3.x--装饰器

时间:2017-06-29 15:17:50      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:time()   running   page   添加   ica   pytho   方式   改变   muse   

————————装饰器=高阶函数+嵌套函数——————————

高阶函数: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--装饰器

标签:time()   running   page   添加   ica   pytho   方式   改变   muse   

原文地址:http://www.cnblogs.com/rainowl-ymj/p/7094368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!