1. iterators
An iterator is a way of accessing a collection of data that can only be accessed sequentially from the first element of the collection until the last element ends. Similar to the Cat command in Linux, can only read text content, can not jump to the middle or tail read (not all the data are read into memory), for the file on the G, compared to save memory. The advantage of this iteration is that it does not need to have all the elements in the collection in advance, and that the element is evaluated when it is traversed, which is very different from the list.
Example 1:
A = ITER (['add','dfdf','dfdfd' ])print a.next ()# takes the first value of print a.next ()# to take the second value Print a.next ()# takes a third value # a = ITER ([' Add ', ' dfdf ', ' DFDFD ')
The iterator keyword ITER (), when reading a collection element, can only be read one at a time using the next () method, which reads a value at random.
Example 2:
For line in open ("Test.txt"). ReadLines ():
Print Line
And
for in open ("test.txt"): #usefile iterators Print Line
Difference:
For lines in open ("test.txt"). The ReadLines () method is to read the entire file into memory and then read each line;
The For lines in open ("Test.txt") method uses iterators to read one line at a time.
2. Generator
The generator is also an iterator that has the next () method and behaves exactly the same as an iterator. When a function contains the yield keyword, the function is a generator function. The yield here is equivalent to return in the normal function, except that yield returns a generator. The generator function is called only when the next () method is executed for the first time. When the function executes to the yied statement, it is aborted and returned with the yield parameter as the return value of the next () method. Each time the next () method is called, the generator function then proceeds to the last aborted yield statement until the yield statement is encountered and then aborted.
Example:
defGet_number ():#1#4 yield0#5#7 yield1#8#10 yield2#11#12a= Get_number ()#2PrintA.next ()#3PrintA.next ()#6PrintA.next ()#9
The first time the program executes to the print a.next () statement, the Get_number function is called, and the function executes to yield 0 o'clock returns the parameter 0 after yield and aborts the function execution. Then the program continues to execute the second sentence print a.next (), continues to call the Get_number function, then executes the yield 1 statement to return 1, then aborts execution of the function body, and then proceeds to execute the third sentence print a.next () ...
Example: single-thread concurrency operation
Import TimedefConsumer (name):Print("%s ready to eat buns!"%name) whileTrue:baozi=yield Print("Bun [%s] came, eaten by [%s]!"%(baozi,name))defproducer (name): C= Consumer ('A') C2= Consumer ('B') c.__next__() c2.__next__() Print("Lao Tzu began to prepare steamed buns!") forIinchRange (10): Time.sleep (1) Print("made 2 buns!") c.send (i) c2.send (i) Producer ("Ahaii")
View Code
3, the decorative device
Example:
def Login (func): Print ' Login ' return func# returns only the memory address of the Func function and does not invoke the Funcdef TV (name): Print'%s to tvpage' %= Login (TV) f (' Ahaii')
Python Quest Path 3--iterators, adorners, generators, regular