装饰器总结
admin
2023-07-20 00:21:47
0次

import time

装饰器定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能

装饰器元祖:1、不能修改被装饰的函数源代码。 2、不能修改不装饰的函数的调用方式。

装饰器 函数内存地址不变,函数名变了其结果不变!!!

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.函数即“变量”

2.高阶函数:1、把一个函数名当做实参传给另外一个函数(在不修改被装饰函数

源代码的情况下为其添加功能)

2、返回值中包含函数名(不修改函数的调用方式)

3.函数嵌套

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

先声明定义再调用!!!

#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()),或者赋值给一个变量,输出那个变量!

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...