1 " ". In Python, this side loop computes the mechanism, called the generator: Generator. 2 There are a number of ways to create a generator. 3 you can get the next return value for generator with the next () function" "4 5 deffib (max):6N, a, b = 0, 0, 17 whileN <Max:8 #print (b)9 yieldbTenA, B = B, A +b Onen = n + 1 A return ' Done' - - the #_*_coding:utf-8_*_ - #@__author__ = ' Alex Li ' - - Import Time + defConsumer (name): - Print("%s ready to eat buns!"%name) + whileTrue: ABaozi =yield at - Print("Bun [%s] came, eaten by [%s]!"%(baozi,name)) - - - defproducer (name): -c = Consumer ('A') inC2 = Consumer ('B') -C.__next__()#This allows the generator to be enabled toC2.__next__()#This allows the generator to be enabled + Print("Lao Tzu began to prepare steamed buns!") - forIinchRange (10): theTime.sleep (1) * Print("made 2 buns!") $ c.send (i)Panax Notoginseng c2.send (i) - theProducer"Alex") + A #implementing concurrent parallel operations with generators the + #iterators - """all objects that can be used for a For loop are iterable (iterative) types; $ all objects that can be used for the next () function are iterator (iterator) types, which represent a sequence of lazy computations; $ collection data types such as list, dict, str, etc. are iterable but not iterator, but can be via ITER () - The if function obtains a iterator object: - """
Python learning-iterators and generators