迭代器
for temp in [11,22,33]:
print(temp)
列表 数组 字符串 元祖
for temp in 'abcdef':
print(temp)
查看目标是否可迭代
from collections import Iterable
print(isinstance([],Iterable))
a = 0 b = 1 a,b = b,a+b
print(a)
a = (11,22,33)
print(list(a))
生成器
第一种方式
nums = (x*2 for x in range(10))
print(nums)
for num in nums:
print(num)
from collections import Iterable
from collections import Iterator
import time
class Classmate(object):
def __init__(self):
self.names = list()
def add(self,name):
self.names.append(name)
def __iter__(self):
# 如果想要一个对象成为一个可以迭代的对象,那么必须实现__iter__方法
# return classIterator(self)
return self
pass
class ClassIterator(object):
def __init__(self,obj):
self.obj = obj
self.current_num = 0
def __iter__(self):
pass
def __next__(self):
if self.current_num < len(self.obj.names):
ret = self.obj.names[self.current_num]
self.current_num += 1
return ret
else:
# 主动抛出异常
raise StopIteration
classmate = Classmate()
classmate.add('小明')
classmate.add('小红')
classmate.add('小亮')
for name in classmate:
print(name)
time.sleep(1)
print('判断classmate是否是可以迭代的对象:',isinstance(classmate,Iterable))
classmate_iterator = iter(classmate)
print('判断classmate是否是可以迭代的对象:',isinstance(classmate,Iterable))
print(next(classmate_iterator))
# for temp in xxx_obj:
# pass
'''
列表、元组、字典、集合、字符串都是可迭代的
一个类中有__iter__就可以迭代,既有__iter__、又有__next__就可以称为迭代器
1、判断xxx_obj是否是可以迭代
看对象的类中是否有__iter__方法
2、在第一步成立的前提下,调用iter函数,得到xxx_obj的返回值
调用iter函数会自动调用__iter__方法
3、__iter__方法的返回值就是一个迭代器
for循环会自动调用迭代器的__next__方法,调一次取一个赋给temp
'''
# 迭代器的优点:迭代器能实现for,能取里边的数据,但它里边存的不是生成这个序列的结果,而是生成这个序列的方式,占用空间极小,这就是迭代器的优点
斐波那契
# nums = list()
#
# a = 0
# b = 1
#
# i = 0
# while i<10:
# nums.append(a)
# a,b = b, a+b
# i+=1
#
# for num in nums:
# print(num)
#
class Fibonacci(object):
def __init__(self,all_num):
self.all_num = all_num
self.current_num = 0
self.a = 0
self.b = 1
def __iter__(self):
return self
def __next__(self):
if self.current_num < self.all_num:
ret = self.a
self.a , self.b = self.b , self.a + self.b
self.current_num += 1
return ret
else:
raise StopIteration
fibo = Fibonacci(10)
for num in fibo:
print(num)
yield、greenlet、gevent
def create_num(all_num):
print('--------1---------')
a,b = 0,1
create_num = 0
while create_num < all_num:
print('--------2---------')
# print(a)
yield a # 一个函数中有yield语句,那么这个函数就不是普通函数,而是一个生成器的模板
print('--------3---------')
a,b = b , a+b
create_num += 1
print('--------4---------')
# 如果在调用函数是,发现函数中有yield,此时不再是调用函数了,而是创建一个生成器对象
obj = create_num(10)
obj2 = create_num(2)
ret = next(obj)
print('obj:',ret)
ret = next(obj)
print('obj:',ret)
ret = next(obj2)
print('obj:',ret)
ret = next(obj)
print('obj:',ret)
ret = next(obj)
print('obj:',ret)
ret = next(obj)
print('obj:',ret)
#
# for num in obj:
# print(num)
def create_num(all_num):
print('--------1---------')
a,b = 0,1
create_num = 0
while create_num < all_num:
print('--------2---------')
# print(a)
yield a # 一个函数中有yield语句,那么这个函数就不是普通函数,而是一个生成器的模板
print('--------3---------')
a,b = b , a+b
create_num += 1
print('--------4---------')
# 如果在调用函数是,发现函数中有yield,此时不再是调用函数了,而是创建一个生成器对象
obj2 = create_num(2)
# 解决超出生成器报错
while True:
try:
ret = next(obj2)
print('obj2',ret)
except Exception as ret:
break
def create_num(all_num):
a,b = 0 ,1
create_num = 0
while create_num < all_num:
ret = yield a
print(">>>ret>>>",ret)
a,b = b, a+b
create_num += 1
obj = create_num(10)
# 如果已创建生成器就send会报错,因为没有yield,也没有ret去接收,但是可以send(None)
ret = next(obj)
print(ret)
ret = obj.send('hahah')
print(ret)
# i = 0
# while i<10:
# ret = next(obj)
# print(ret)
import time
def task_1():
while True:
print('--1--')
time.sleep(0.1)
yield
def task_2():
while True:
print('--2--')
time.sleep(0.1)
yield
def main():
t1 = task_1()
t2 = task_2()
while True:
next(t1)
next(t2)
if __name__ == '__main__':
main()
# 并行 真多任务
# 并发 假多任务
from greenlet import greenlet
import time
def test1():
while True:
print ("---A--")
gr2.switch()
time.sleep(0.5)
def test2():
while True:
print ("---B--")
gr1.switch()
time.sleep(0.5)
gr1 = greenlet(test1)
gr2 = greenlet(test2)
#切换到gr1中运行
gr1.switch()