Production is

1  '' ' 
2  Builder:
 3  concepts: yield using a function generator function is called. Generator
 . 4  with the difference between ordinary functions: a generator function is a function returns an iterator,
 5  only for an iterative operation. You can think generator is a iterator.
6  '' ' 
7  # system builder 
. 8 in Li1 = (X for X in Range (. 3))    # [0,1,2] 
. 9  Print (in Li1)
 10  Print (type (in Li1))   # Generator 
. 11  
12 is LI2 = (X * 2 for X in Range (. 4))   # [0, 2,. 4,. 6] 
13 is  for I in LI2:
14     print(i)
15 
16 li3 = (3 for x in range(3))
17 print(list(li3))
18 
19 
20 li4 = (x for x in range(11) if x % 2 == 0)
21 print(list(li4))
22 
23 l = []
24 for i in range(11):
25     if i % 2 == 0:
26         l.append(i)
27 print(l)
28 
29 
30  # function 
31 is  DEF func1 ():
 32      Print ( " --1-- " )
 33 is      Print ( " --2-- " )
 34 is      Print ( " --3-- " )
 35  func1 ()
 36  Print (type (func1))   # function 
37 [  Print (type (func1 ()))    # NoneType 
38 is  
39  # generator functions 
40  DEF func2 ():
 41 is      Print ( " *** 111 ***")
42     print(3456789)
43     yield
44     print("***222***")
45     yield
46     print("***333***")
47     yield
48 print(type(func2))  # function
49 print(type(func2()))   # generator
50 print('----------------------------------------')
51 a = func2()
52 next(a)
53 next(a)
54 next(a)
55 # next(a)   # StopIteration
56 
57 def func3():
58     print("---111---")
59     yield "a"
60     print("---222---")
61     yield "b"
62     print("---333---")
63     yield
64 
65 b = func3()
66 print(next(b))
67 print(next(b))
68 print(next(b))   # None

 

Guess you like

Origin www.cnblogs.com/BKY88888888/p/11266047.html