import time
网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、成都微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了大安免费建站欢迎大家使用!
def timmer(func): # 这就是装饰器!
def warpper(*args,*kwargs):
start_time = time.time()
func(args,**kwargs)
stop_time = time.time()
print("the func run time is %s" % (stop_time - start_time))
return warpper
@timmer #函数的内存地址赋予(把timmer这个装饰器的内存地址给调用他的函数)
def test():
time.sleep(3)
print("test1")
test()
#1
def fff():
print("llllllll")
xxx()
def xxx():
print("ccococococ")
fff()
#2
def xxx():
print("ccococococ")
def fff():
print("llllllll")
xxx()
fff()
#3 调用xxx的时候后,xxx没有先定义!
def fff():
print("llllllll")
xxx()
fff()
def xxx():
print("ccococococ")
def timmer(func): # 这就是装饰器!
def warpper():
start_time = time.time()
func()
stop_time = time.time()
print("the func run time is %s" % (stop_time - start_time))
return warpper
def test():
time.sleep(3)
print("test1")
timmer(test)
def timmer(func): # 这就是装饰器!
def warpper():
start_time = time.time()
func()
stop_time = time.time()
print("the func run time is %s" % (stop_time - start_time))
return warpper
@timmer #函数的内存地址赋予(把timmer这个装饰器的内存地址给调用他的函数)
def test():
time.sleep(3)
print("test1")
test()
def timmer(func): # 这就是装饰器!
def warpper(*args,*kwargs):
start_time = time.time()
func(args,**kwargs)
stop_time = time.time()
print("the func run time is %s" % (stop_time - start_time))
return warpper
@timmer #函数的内存地址赋予(把timmer这个装饰器的内存地址给调用他的函数)
def test(): # @timmer=test
time.sleep(3)
print("test1")
test("alex")
user,keypass="hy","123"
def xxxx(func):
def wapper(*args, *kwargs):
username = input("username:").strip()
pasword = input("pssword:").strip()
if user == username and keypass == pasword:
print("welcome to logging---。。。。")
rrs = func(args, **kwargs)
return rrs
else:
print("username or passord is no")
return wapper
def index():
print("welcome to index")
return 0@xxxx
br/>@xxxx
print("welcome to home ")
return "alex is pig " # 函数的返回值需要输出,home()调用不会输出返回值。@xxxx
br/>@xxxx
print("welcome to bbs")
index()
print(home())
bbs()
user,passwd="alex","123"
def auth(aupe):
def out_warpper(func):
def wrapper(*args, *kwargs):
if aupe == "local":
username = input("username:").strip()
password = input("password:").strip()
if user == username and passwd == password:
print("\033[32;1muser has passed authentication\033[0m")
res = func(args, **kwargs)
return res
else:
print("\033[32;1mInvalid username or password\033[0m")
elif aupe == "country":
print("搞毛线ldap,不会")
else:print("city 欢迎你")
return wrapper
return out_warpper@auth(aupe="city")
br/>@auth(aupe="city")
print("welcome to city!")@auth(aupe="country")
br/>@auth(aupe="country")
print("welcome to country!!")@auth(aupe="local")
br/>@auth(aupe="local")
print("welcome to home!!! %name")
acct=acc-name
return "from there",acct
city()
country()
local()
print(local())# 函数的返回值需要print函数的调用(local()),或者赋值给一个变量,输出那个变量!
文章名称:装饰器总结
URL分享:https://www.cdcxhl.com/article42/psdoec.html
成都网站建设公司_创新互联,为您提供搜索引擎优化、网站建设、手机网站建设、App设计、软件开发、动态网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联