. 1 from Collections Import the Iterable 2 from Collections Import the Iterator . 3 # as spoken blog entry, generator may be written as follows, when the generator is next () call or cycle, where the generator is running to yield statement, followed by variable yield the value assigned to yield, yield will return . 4 DEF str_ip (ip_start): . 5 for IP1 in Range (256 ): . 6 for IP2 in Range (256 ): . 7 for IP3 in Range (256 ): . 8 ip_str_format = " % S .% S.% S.% S " %(STR (ip_start), STR (IP1), STR (IP2), STR (IP3)) . 9 the yield ip_str_format 10 return " the this IS DONE ITER " # iterator return here is abnormal when the value of the throw . 11 12 is # Function it calls assigned to a variable, the variable is at this time the generator 13 is str_ip_gen = str_ip (66 ) 14 # using generators next method to obtain data 15 for I in Range (30 ): 16 Print (str_ip_gen. __next__ () ) . 17 18 is # the following code uses the send method of the iterator value passed from the outside to achieve the parallel operation results yield . 19 DEF Consumer (name): 20 is Print( " Preparation of the bun " ) 21 is the while True: 22 is # where variables are assigned directly yield, to achieve the effect of traditional values outside of the generator 23 is Baozi = the yield 24 Print ( " % s% s eat buns " % ( name, Baozi)) 25 26 is # define producer, the producer is used to generate the send method to pass data to the consumer 27 DEF producer (): 28 # configuration of the two generators 29 CON1 = consumer ( " XXX " ) 30 CON2 = Consumer ( " yyy " ) 31 #Call next time, so that the first generator come to yield position, then the next call data preparation 32 CON1. __Next__ () 33 is CON2. __Next__ () 34 is # cycles to yield bun 35 for J in [ " chives " , " mushrooms " , " tofu " ]: 36 # calling the send method herein generator passes the value of the yield, and generates a downward iterations. 37 [ con1.send (J) 38 is con2.send (J) 39 Producer () 40 41 is # iterators 42 #It may be useful for the following cycle: 1, set type: a set of tuples string dictionaries 2 and the like, generator; may be used for opening a loop iteration objects can, using the isinstance () Analyzing 43 is Print (the isinstance ({}, the iterable)) # True, such conditions are satisfied iterable 44 # can be called the next method is called and returns an iterator continue next value, whether an object is determined by the following statement iterators. Generator must iterators meet iterator definition 45 Print (the isinstance ({}, the Iterator)) # False 46 is # listing dictionaries are iterables but not iterator using iter method may be iterative object into iteration an 47 Print (isinstance (iter ({}), iterator)) # True 48 49 "" " 50 Why dictionaries list iterator iterable not it? python is iterator object represents one data stream iterator object next method to get the next value in the data stream; 51 . StopIteration throw until there is no data error can be seen as a data stream ordered set but can not predict in advance of their length, you can only go down through the next method. 52 iterators are inert, only one call was take a step backwards, but it can represent an infinite data, and a list of dictionaries and other non-iterator can not express the infinite content, such as all positive integers 53 "" "