python之装饰器的进阶
一、带参数的装饰器 (必须会)
针对不同的app的验证,比如:天猫和天猫超市,京东商城和京东超市
def wrapper_out(n):
print(n)
def wrapper(f):
def inner(*args,**kwargs):
if n == '腾讯':
user_input = input('请输入用户名: ').strip()
pass_input = input('请输入密码: ').strip()
with open('qq.txt',mode='r+',encoding='utf-8') as f1:
for i in f1:
username,password = i.strip().split('|')
if username == user_input and password == pass_input:
print('登录成功')
ret = f(*args,**kwargs)
return ret
return False
elif n == '抖音':
user_input = input('请输入用户名: ').strip()
pass_input = input('请输入密码: ').strip()
with open('dy.txt',mode='r+',encoding='utf-8') as f2:
for j in f2:
username, password = j.strip().split('|')
if username == user_input and password == pass_input:
print('登录成功')
ret = f(*args, **kwargs)
return ret
return False
return inner
return wrapper
@wrapper_out('腾讯')
def qq():
print('成功访问qq')
qq()
@wrapper_out('抖音')
def tiktok():
print('成功访问抖音')
tiktok()
函数一般嵌套3层
def wrapper_out(n):
def wrapper(f):
def inner(*args,**kwargs):
with open(n,encoding = 'utf-8') as f1:
for line in f1:
user,pass = line.strip().split('|')
username = input('请输入用户名: ').strip()
password = input('请输入密码: ').strip()
if username == user and passwor == pass:
dic_status['status'] = True
ret = f(*args,**kwargs)
return ret
看到带参数的装饰器分两步执行:
1.执行warpper_out('腾讯')这个函数,把相应的参数'腾讯'传给n,并且得到返回值warpper
2.与@warpper结合,得到我们之前熟悉的标准版的装饰器按照装饰器流程执行
练习题
# 如果输入的是抖音的账号和密码就访问抖音,如果输入的是腾讯的用户名和密码就访问腾讯
def wrapper_out(n):
print(n)
def wrapper(f):
def inner(*args,**kwargs):
if n == '腾讯':
user_input = input('请输入用户名: ').strip()
pass_input = input('请输入密码: ').strip()
with open('qq.txt',mode='r+',encoding='utf-8') as f1:
for i in f1:
username,password = i.strip().split('|')
if username == user_input and password == pass_input:
print('登录成功')
ret = f(*args,**kwargs)
return ret
return False
elif n == '抖音':
user_input = input('请输入用户名: ').strip()
pass_input = input('请输入密码: ').strip()
with open('dy.txt',mode='r+',encoding='utf-8') as f2:
for j in f2:
username, password = j.strip().split('|')
if username == user_input and password == pass_input:
print('登录成功')
ret = f(*args, **kwargs)
return ret
return False
return inner
return wrapper
@wrapper_out('腾讯')
def qq():
print('成功访问qq')
qq()
@wrapper_out('抖音')
def tiktok():
print('成功访问抖音')
tiktok()
增强耦合性写法
def wrapper_out(n):
def wrapper(f):
def inner(*args,**kwargs):
user_input = input('请输入用户名: ').strip()
pass_input = input('请输入密码: ').strip()
with open(n,mode='r',encoding='utf-8') as f1:
for i in f1:
username,password = i.strip().split('|')
if username == user_input and password == pass_input:
print('登录成功')
ret = f(*args,**kwargs)
return ret
else:
print('验证失败')
return False
return inner
return wrapper
@wrapper_out('qq')
def qq():
print('成功访问qq')
qq()
@wrapper_out('dy')
def tiktok():
print('成功访问抖音')
tiktok()
二、多个装饰器装饰一个函数 (流程会)
def wrapper1(func1): # func1 = f原函数
def inner1():
print('wrapper1 ,before func') # 2
func1()
print('wrapper1 ,after func') # 4
return inner1
def wrapper2(func2): # func2 == inner1
def inner2():
print('wrapper2 ,before func') # 1
func2() # inner1
print('wrapper2 ,after func') # 5
return inner2
@wrapper2 # f = wrapper2(f) 里面的f == inner1 外面的f == inner2
@wrapper1 # f = wrapper1(f) 里面的f == func1 外面的 f == inner1
def f():
print('in f') # 3
f() # inner2()
# 输出结果
wrapper2 ,before func
wrapper1 ,before func
in f
wrapper1 ,after func
wrapper2 ,after func
三、递归函数 (练习题,作业题会做*)
递归就是自己用自己
官网规定: 默认递归的最大深度1000次
如果你递归超过100次还没有解决这个问题,那么执意使用递归,效率很低
形式
def func(n):
print(n)
n += 1
func(n)
func(1)
练习题
# 查看电脑执行次数
import sys
print(sys.setrecursionlimit(1000000))
def func(n):
print(n)
n += 1
func(n)
func(1)
def age(n):
if n == 1:
return 18
else:
return age(n-1) + 2
print(age(4))
# 输出结果
24
#
l1 = [1, 3, 5, ['太白','元宝', 34, [33, 55, [11,33]]], [77, 88],66]
def func(n):
for i in n:
if type(i) == list:
func(i)
else:
print(i)
func(l1)
# 输出结果
1
3
5
太白
元宝
34
33
55
11
33
77
88
66