x="hello"
#print(dir(x))
iter_test=x.__iter__()
#print(iter_test)
# print(iter_test.__next__())
# print(iter_test.__next__())
#可迭代对象就是迭代器
l=[1,2,3]
# for i in l:#先执行i=l.__iter__() ,i.__next__() for循环基于迭代器协议,
# 能被for循环遍历的类型必须具有__iter__方法
# print(i)
#print(l[0])
# iter_l=l.__iter__()#遵循可迭代协议,生成可迭代对象
# print(iter_l.__next__())
#
#
#遵循可迭代协议的数据类型有list,set,dict,str,tuple,和文件操作
# l=[1,2,3]
# for i in l:
# print(i)
#
#
#
# index=0
# while index<len(l):
# print(l[index])
# index+=1
# s={1,2,3}
# for i in s:
# print(i)
# iter_s=s.__iter__()
# print(iter_s)
# print(iter_s.__next__())
# print(iter_s.__next__())
# print(iter_s.__next__())
# dic={'a':1,'b':2}
# iter_d=dic.__iter__()
# print(iter_d.__next__())
# f=open('code.txt','r+')
# # for i in f:
# # print(f)
# iter_f=f.__iter__()
# print(iter_f)
# print(iter_f.__next__(),end='')
# print(iter_f.__next__(),end='')
#t=["tang","han","xiao"]
#iter_t=iter(t)
# iter_t=t.__iter__()
# print(iter_t.__next__())
# #next 方法 ---->iter_t.__next__()
# print(next(iter_t))
#生成器就是可迭代对象
#生成器函数
# def test():
# yield 0
# yield 1
# yield 2
# yield i
# g=test()
# i=10
# print(test())
# print(g.__next__())
# print(g.__next__())
# print(g.send(i))
# print(g.__next__())
#三元表达式
# name="alex"
# res='SB' if name=="alex" else "帅哥"
# print(res)
#列表解析
# egg_list=[]
# for i in range(10):
# egg_list.append("鸡蛋%s"%i)
# print(egg_list)
# a=["鸡蛋%s"%i for i in range(10)]
# #a1=["鸡蛋%s"%i for i in range(10) if i>5]#三元表达式
# print(a)
#生成器表达式
laomuji=a=("鸡蛋%s"%i for i in range(10))
print(laomuji)
print(laomuji.__next__())
print(laomuji.__next__())
print(next(laomuji))
# def test():
# yield 1
# yield 2
# yield 3
# g=test()
# print(g)
# print(g.__next__())
#yield: 1.用来做返回值 2.保留函数的运行状态
#母鸡下蛋
# def egg():
# egg_list=[]
# for i in range(100):
# egg_list.append("鸡蛋%s"%i)
# return egg_list
# print(egg())
#缺点:占用空间大,效率低
def egg():
for i in range(100):
yield "鸡蛋%i" % i
ret=egg()
print(ret.__next__())
print(ret.__next__())
print("xxxxxx")
# for jidan in ret:
# print(jidan)