針對不一樣的app的驗證,好比:天貓和天貓超市,京東商城和京東超市python
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層app
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結合,獲得咱們以前熟悉的標準版的裝飾器按照裝飾器流程執行
練習題code
# 若是輸入的是抖音的帳號和密碼就訪問抖音,若是輸入的是騰訊的用戶名和密碼就訪問騰訊 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次ip
若是你遞歸超過100次尚未解決這個問題,那麼執意使用遞歸,效率很低utf-8
形式input
def func(n): print(n) n += 1 func(n) func(1)
練習題it
# 查看電腦執行次數 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