In the past, I always wanted to concentrate on learning things. After the National Day holiday, I found that this TM was not practical at all. It was better to spend some time every day.
1. circular object
The circular object does not exist with the birth of Python, But it develops rapidly. Especially in the era of Python 3x, circular objects are becoming the standard form of loops.
A circular object is such an object. It contains a next () method (_ next _ () method, in Python 3x ),
The purpose of this method is to proceed to the next result, and after the end of a series of results, the stopiteration error is cited.
When a loop structure (such as for) calls a loop object, it calls the next () method every time it loops until stopiteration appears,
After receiving the for loop, you will know that the loop has ended and stop calling next ().
2. Simulation
Assume that there is a file test.txt with the following content:
123abcxyz
Now let's read the content one by one:
f = open("test.txt")print f.next()print f.next()print f.next()# print f.next() #Traceback...in:...StopIteration
In the last commented print F. Next (), an error is reported, indicating stopiteration.
Open () actually returns a circular object containing the next () method.
The next () method returns the content of a new row each time, and the stopiteration is cited at the end of the file.
This is equivalent to simulating the following loop:
for line in open("test.txt"): print line
Or the structure automatically calls the next () method and assigns the return value of this method to line. The loop will end when stopiteration occurs.
Iii. Generator
The main purpose of generator is to create a user-defined cyclic object.
The Writing Method of the generator is similar to the function definition, but the return is changed to yield.
The generator can have multiple yield instances. When the generator encounters a yield, it will pause running the generator and return the value following yield.
When the generator is called again, it will continue running from the paused place until the next yield.
The generator itself forms a loop, and each cycle uses a value returned by yield.
The following is a generator:
def gen(): a = 2 yield a a = a * 2 yield a yield 1024
The generator has three yield instances. If it is used as a loop generator, it performs three cycles.
for i in gen(): print i
Let's look at another generator:
def gen(): for i in range(4): yield i
It can be written as a generator expression:
G = (x for x in range(4))
Generator expressions are a simple way to compile generators.
Iv. Table Derivation
List comprehension is a quick way to generate tables.
For example:
L = []for b in range(10): L.append(b**2)
The preceding table L is generated, but there is actually a quick writing method, that is, the table deduction method:
L = [b**2 for b in range(10)]print(L)
Output:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Another example:
xl = [1,3,5]yl = [9,12,13]L = [ x**2 for (x,y) in zip(xl,yl) if y > 10]print(L)
Output:
[9, 25]
Pythian Study Notes 6: cyclic objects