迭代器相关
基于迭代器的方式,用while循环迭代取值字符串、列表、元组、字典、集合、文件对象
def for1(l):
l = l.__iter__()
while True:
try:
print(l.__next__())
except:
break
l = 'abcde'
for1(l)
自定义迭代器实现range功能
def range_my(start,end,step = 1):
while True:
if step > 0:
if start < end:
yield start
start += step
else:
break
else:
if start > end:
yield start
start += step
else:
break
g = range_my(0,5,2)
for i in g:
print(i)