摘要: _iter_ 只要是能被for循环的数据类型就一定拥有_iter_方法 例如: print('__iter__' in dir(int)) print('__iter__' in dir(bool)) print('__iter__' in dir(list)) print('__iter__' i 阅读全文
posted @ 2019-08-12 23:15 套你大象 阅读(112) 评论(0) 推荐(0) 编辑
摘要: 在Python中,很多对象都是可以通过for语句来直接遍历的,例如list、string、dict等等,这些对象都可以被称为可迭代对象。 迭代器 迭代器对象要求支持迭代器协议的对象,在Python中,支持迭代器协议就是实现对象的__iter__()和next()方法。其中__iter__()方法返回 阅读全文
posted @ 2019-08-12 22:42 套你大象 阅读(151) 评论(0) 推荐(0) 编辑
摘要: def wrapper1(func): def s1(): print("套你大象1号") func() print("套你大象2号") return s1 def wrapper2(func): def s2(): print("套你大象3号") func() print(... 阅读全文
posted @ 2019-08-08 22:06 套你大象 阅读(100) 评论(0) 推荐(0) 编辑
摘要: 当有很多函数都需要在某一个时间内需要装饰是,通过三层嵌套,可以实现对多个函数进行装饰。 import time FLAGE = False def timeer_out(flag): def timeer(func): def inner(*args,**kwargs): if flag: star 阅读全文
posted @ 2019-08-08 21:17 套你大象 阅读(1385) 评论(0) 推荐(0) 编辑
摘要: import time def timer(f): def innner(*args,**kwargs): '''在被装饰之前要做的事''' ret = f(*args,**kwargs) '''在被装饰之后要做的事''' return ret return innner @timer#语法糖,@装 阅读全文
posted @ 2019-08-05 22:35 套你大象 阅读(110) 评论(0) 推荐(0) 编辑
摘要: 语法糖 import time def timer(f): def innner(): s = time.time() f() e = time.time() print(e - s) return innner @timer#语法糖,@装饰器函数名 def fuc():#被装饰的函数 time.s 阅读全文
posted @ 2019-08-04 23:07 套你大象 阅读(137) 评论(0) 推荐(0) 编辑
摘要: import time def fuc(): time.sleep(0.01) print("大象你好") def timer(f): def innner(): s = time.time() f() e = time.time() print(e - s) return innner fuc= 阅读全文
posted @ 2019-08-04 22:22 套你大象 阅读(85) 评论(0) 推荐(0) 编辑
摘要: 将用户上传的列表或者元组按索引奇数返回对应的元素给用户 def qishu(l): d = [] for index in range(len(l)): if index % 2 ==1: d.append(l[index]) else: pass print(d) qishu([123,5,345 阅读全文
posted @ 2019-08-01 23:21 套你大象 阅读(108) 评论(0) 推荐(0) 编辑
摘要: 闭包 闭包:嵌套函数,内部函数调用外部函数的变量。 def outer(): a = 1 def c(): print(a) print(c.__closure__) outer() 应用 import urllib #模块 from urllib.request import urlopen re 阅读全文
posted @ 2019-08-01 22:31 套你大象 阅读(397) 评论(0) 推荐(0) 编辑
摘要: 函数的嵌套定义 内部函数可以使用外部的函数变量 def tao(): a = 1 def ni(): print(a) return print('houzi') ni() tao() nonlocal a = 1 def tao(): a = 1 def ni(): b = 2 print(a) 阅读全文
posted @ 2019-08-01 22:12 套你大象 阅读(203) 评论(0) 推荐(0) 编辑