11.10.2 加强的生成器特性:

11.10.2 加强的生成器特性:

在python 2.5中,一些加强特性加入到生成器中,所以除了next()来获得下个生成的值,

用户将值会送给生成器中,所以除了next()来获得下个生成的值,用户可以将值回送给生成器[send()]

def counter(start_at=0):
   count = start_at
   while True:
     val = (yield count)
     if val is not None:
       count = val
     else:
        count += 1
a=counter()
print a.next()
print a.next()
print a.next()
print a.next()
print a.next()
print a.send(2)

C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/eee/a20.py
0
1
2
3
4
2

原文地址:https://www.cnblogs.com/hzcya1995/p/13349224.html