""" 生成器 yield """ import sys def yield_demo1(): l = [x for x in range(5)] return l r = yield_demo1() print(r) s = range(5) print(s) # print(sys.getsizeof(r)) def yield_demo2(): for x in range(3): yield x # print("yield 生成器") # print("生成器外层") def yield_demo2(): for x in ["123", "123sdf", 1.5, 1.7]: yield x print("yield 生成器" + str(x)) print("生成器外层") # a = yield_demo2() # print(sys.getsizeof(a)) # print(a.__next__()) # print(a.__next__()) # print(a.__next__()) # print(a.__next__()) # for i in a: # print(i) # print(sys.getsizeof(0)) # print(sys.getsizeof(str())) # print(sys.getsizeof(int())) # print(sys.getsizeof(1)) # print(sys.getsizeof(1423)) # print(sys.getsizeof(14232342343242342343242384723987498237589238482736148972136846238746872364812902348324)) # 65535 byte # 多生成器的例子 def yield_demo3(): a = 10 b = 20 c = 30 for x in range(3): # range(3) 那么就是从0开始到3,左闭右开 0,1,2 range(1,3) 1,2 yield a print("生成器返回a之后") yield b print("生成器返回b之后") yield c print("生成器返回c之后") g= yield_demo3() print(g.__next__()) print(g.__next__()) print(g.__next__()) print(g.__next__()) for a in g: print(a) print(sys.getsizeof(0)) print(sys.getsizeof(str())) print(sys.getsizeof(int())) print(sys.getsizeof(1)) print(sys.getsizeof(1423)) print(sys.getsizeof(1423424123123123)) # 65535 byte
Python 中的列表推导式
最新推荐文章于 2024-08-16 23:45:00 发布