day12...................... 有参装饰器, 迭代器
1 有参装饰器.
1 ''' 2 3 import time 4 5 current_user={'user':None} 6 7 def deco(func): 8 def wrapper(*args,**kwargs): 9 if current_user['user']: 10 #已经登陆过 11 res = func(*args, **kwargs) 12 return res 13 user=input('username>>: ').strip() 14 pwd=input('password>>: ').strip() 15 if user == 'egon' and pwd == '123': 16 print('login successful') 17 # 记录用户登陆状态 18 current_user['user']=user 19 res=func(*args,**kwargs) 20 return res 21 else: 22 print('user or password error') 23 return wrapper 24 25 @deco 26 def index(): 27 print('welcome to index page') 28 time.sleep(1) 29 30 @deco 31 def home(name): 32 print('welecome %s to home page' %name) 33 time.sleep(0.5) 34 35 36 index() 37 home('egon') 38 ''' 39 40 ''' 41 42 def f1(): 43 x=1 44 def f2(): 45 def f3(): 46 print(x) 47 return f3 48 return f2 49 50 f2=f1() 51 52 f3=f2() 53 54 f3() 55 56 ''' 57 58 59 60 61 import time 62 current_user={'user':None} 63 def auth(engine='file'): 64 def deco(func): 65 def wrapper(*args,**kwargs): 66 if current_user['user']: 67 #已经登陆过 68 res = func(*args, **kwargs) 69 return res 70 user=input('username>>: ').strip() 71 pwd=input('password>>: ').strip() 72 if engine == 'file': 73 # 基于文件的认证 74 if user == 'egon' and pwd == '123': 75 print('login successful') 76 # 记录用户登陆状态 77 current_user['user']=user 78 res=func(*args,**kwargs) 79 return res 80 else: 81 print('user or password error') 82 elif engine == 'mysql': 83 print('基于mysql的认证') 84 elif engine == 'ldap': 85 print('基于ldap的认证') 86 else: 87 print('无法识别认证来源') 88 return wrapper 89 return deco 90 91 @auth(engine='mysql') # @deco #index=deco(index) #index=wrapper 92 def index(): 93 print('welcome to index page') 94 time.sleep(1) 95 96 @auth(engine='mysql') 97 def home(name): 98 print('welecome %s to home page' %name) 99 time.sleep(0.5) 100 101 102 index() 103 home('egon')
2 迭代器
''' 1、什么是迭代器 迭代器即迭代取值的工具 迭代: 迭代是一个重复的过程,每一次重复都是基于上一次的结果而来的 单纯的重复并不是迭代 while True: print('1111') 迭代: l=['a','b','c'] def iterator(item): i=0 while i < len(item): print(l[i]) i+=1 2、 为什么要有迭代器 基于索引的迭代器取值方式只适用于列表、元组、字符串类型 而对于没有索引的字典、集合、文件,则不在适用 所以必须找到一种通用的并且不依赖于索引的迭代器取值方式=》迭代器 迭代器适用于可迭代的类型 3、如何用迭代器 ''' # l=['a','b','c'] # i=0 # while i < len(l): # print(l[i]) # i+=1 # l = ['a', 'b', 'c'] # s='hello' # # def iterator(item): #item='hello' # i = 0 # while i < len(item): # print(item[i]) # i += 1 # # iterator(l) # iterator(s) # 可迭代的对象:在python中但凡内置有__iter__方法的对象都是可迭代的对象 # 字符串、列表、元组、字典、集合、文件都是可迭代的对象 # num1=10 # num2=10.1 # s1='hello' # l=[1,2,3] # t=(1,2,3) # d={'x':1} # s2={1,2,3} # f=open('a.txt','w') # # s1.__iter__ # l.__iter__ # t.__iter__ # d.__iter__ # s2.__iter__ # f.__iter__ # # # 迭代器对象:指的是既内置有__iter__方法,又内置有__next__方法的对象 #执行可迭代对象的__iter__方法得到的就是内置的迭代器对象 # 文件对象本身就是迭代器对象 #强调: #1、迭代器对象一定是可迭代的对象,反之则不然 # info={'name':'egon','age':18,'is_beautiful':True,'sex':'male'} # info_iter=info.__iter__() # # print(info_iter) # # res1=info_iter.__next__() # print(res1) # # res2=info_iter.__next__() # print(res2) # # res3=info_iter.__next__() # print(res3) # # res4=info_iter.__next__() # print(res4) # # info_iter.__next__() # 一旦迭代器取值取干净,再继续取就会抛出StopIteration # info={'name':'egon','age':18,'is_beautiful':True,'sex':'male'} # # info=[1,2,3,4,5] # info_iter=info.__iter__() # while True: # try: # print(info_iter.__next__()) # except StopIteration: # break #for循环:迭代器循环 # info={'name':'egon','age':18,'is_beautiful':True,'sex':'male'} #in后跟的一定要是可迭代的对象 # for k in info: # info_iter=info.__iter__() # print(k) # f=open('a.txt','r') # for k in f: # print(k) # 迭代器对象:指的是既内置有__iter__方法,又内置有__next__方法的对象 # 执行迭代器对象的__next__得到的是迭代器的下一个值 # 执行迭代器对象的__iter__得到的仍然是迭代器本身 # iter_info=info.__iter__() # # print(iter_info) # print(iter_info is iter_info.__iter__() is iter_info.__iter__().__iter__().__iter__().__iter__().__iter__()) # #总结迭代器对象的优缺点: #优点: #1、提供了一种通用的、可以不依赖索引的迭代取值方式 #2、迭代器对象更加节省内存 # f=open('movie.tar.gz','rb') # f.__ next__() # f=open('db.txt','rt',encoding='utf-8') # # print(f.__next__()) # print(f.__next__()) # print(next(f)) #f.__next__() # s='hello' # print(s.__len__()) # print(len(s)) # s.__iter__() # print(iter(s)) # 缺点: #1、迭代器的取值不如按照索引的方式更灵活,因为它只能往后取不能往前退 #2、无法预测迭代器值的个数 # names=['egon','alex_SB','wxx_SB'] # iter_names=iter(names) # print(next(iter_names)) # print(next(iter_names)) #