023_decorater
Posted liujun5319
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了023_decorater相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python
# Author:liujun
# The essence of decorator is function which is used to decorate another function.
# The decorator is used to add some additional ability to a function.
# The principle of defining a decorator
# You can not modify the source code of the function to be decorated
# You can not change the way calling the function to be decorated
# Functions are alse variables
# Higher-order functions
# Pass a function as a parameter to another function
# The function name is included in the return value
# The following code implements adding a functionality to the function,
# but it‘s not a decorator,because it doesn‘t satisfy the other condition
import time
def func():
time.sleep(1)
print("in the func")
def decorator(func):
start_time = time.time()
func()
end_time = time.time()
print("the func run time is %s"%(end_time - start_time))
#decorator(func)
# The following code implements adding functionalty to a function without changing the way to call it
def bar():
time.sleep(1)
print("in the bar")
def decorator2(func):
print(func)
return func # Just return the address of func
bar = decorator2(bar)
#bar()
# Embeded function
def foo():
print("in the foo")
def bar(): # This is a local variable
print("in the bar")
bar()
foo()
# Decoretor version 1
# We do not modify the source code and we add the new func to it.
# But we changed the way to call it
def decorator1(func):
start_time = time.time()
func()
end_time = time.time()
print("the func run time is %s" % (end_time - start_time))
def test1_1():
time.sleep(1)
print("in the test1_1")
def test1_2():
time.sleep(1)
print("in the test1_2")
decorator1(test1_1)
decorator1(test1_2)
# Decoretor version 2
# We do not modify the source code and not change the way to call it
# But we do not add new func to it
def decorator2(func):
start_time = time.time()
return func
end_time = time.time()
print("the func run time is %s" % (end_time - start_time))
def test2_1():
time.sleep(1)
print("in the test2_1")
def test2_2():
time.sleep(1)
print("in the test2_2")
test2_1 = decorator2(test2_1)
test2_2 = decorator2(test2_2)
test2_1()
test2_2()
# Decoretor version 3 : higher-order function + Embed function
def timer(func):
def deco():
start_time = time.time()
func()
end_time = time.time()
print("the func run time is %s" % (end_time - start_time))
return deco
def test3_1():
time.sleep(1)
print("in the test3_1")
def test3_2():
time.sleep(1)
print("in the test3_2")
test3_1 = timer(test3_1)
test3_2 = timer(test3_2)
test3_1()
test3_2()
# Decoretor version 4 : higher-order function + Embed function + @decoretor
def timer2(func):
def deco():
start_time = time.time()
func()
end_time = time.time()
print("the func run time is %s" % (end_time - start_time))
return deco
@timer2 # @timer2 is identical to test4-1 = timer(test4-1)
def test4_1():
time.sleep(1)
print("in the test4_1")
@timer2 # @timer2 is identical to test4-2 = timer(test4-2)
def test4_2():
time.sleep(1)
print("in the test4_2")
test4_1()
test4_2()
# Decoretor version 4 : higher-order function + Embed function + @decoretor + parameters
def timer3(func):
def deco(*args,**kwargs):
start_time = time.time()
func(*args,**kwargs)
end_time = time.time()
print("the func run time is %s" % (end_time - start_time))
return deco
@timer3
def test5_1():
time.sleep(1)
print("in the test5_1")
@timer3 # @timer2 is identical to test4-2 = timer(test4-2)
def test5_2(name,age):
time.sleep(1)
print("in the %s : %s"%(name,age))
test5_1()
test5_2("liujun",28)
#!/usr/bin/env python
# Author:liujun
user = "liujun"
passwd = "123456"
‘‘‘
#version 1
def auth(func):
def inDecoretor(*args,**kwargs):
username = input("username:")
password = input("password:")
func(*args,**kwargs)
return inDecoretor
@auth
def home():
print("this is home")
#return "from home"
@auth
def bbs():
print("this is bbs")
‘‘‘
#version 2
def auth(autoType):
def outDecoretor(func):
def inDecoretor(*args, **kwargs):
username = input("username:")
password = input("password:")
res = "none"
if autoType=="home":
if user == username and passwd == password:
res = func(*args, **kwargs)
else:
print("your name or password are wrong")
else:
print("bbs can not be allowed here")
return res
return inDecoretor
return outDecoretor
@auth("home")
def home():
print("this is home")
return "from home"
@auth("bbs")
def bbs():
print("this is bbs")
print(home())
bbs()
以上是关于023_decorater的主要内容,如果未能解决你的问题,请参考以下文章