Python advanced tutorial loop object, python advanced tutorial object
The main purpose of this lecture is to give you a basic idea of circular objects while reading Python programs.
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.
What is a circular object?
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 the StopIteration occurs and the for loop receives, you will know that the loop has ended and stop calling next ().
Suppose we have a test.txt file:
Copy codeThe Code is as follows:
1234
Abcd
Efg
Run the following python command line:
Copy codeThe Code is as follows:
>>> F = open('test.txt ')
>>> F. next ()
>>> F. next ()
...
Enter f. next () until the StopIteration occurs.
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. In this way, we perform a loop manually.
If it is automatically executed, it is:
Copy codeThe Code is as follows:
For line in open('test.txt '):
Print line
Here, the for structure automatically calls the next () method and assigns the return value of this method to line. The loop will end when StopIteration occurs.
Compared with sequences, the benefit of using cyclic objects is that you do not need to generate the elements to be used before the loop starts. The elements used can be generated in a loop. In this way, space is saved, efficiency is improved, and programming is more flexible.
Iterator
Technically, there is also an intermediate layer between the loop object and the for loop call, which is to convert the loop object into an iterator ). This conversion is implemented by using the iter () function. However, this layer can be ignored logically, so loop objects and iterators often refer to each other.
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:
Copy codeThe Code is as follows:
Def gen ():
A = 100
Yield
A = a * 8
Yield
Yield 1000
The generator has three yield instances. If it is used as a loop generator, it performs three cycles.
Copy codeThe Code is as follows:
For I in gen ():
Print I
Consider the following generator:
Copy codeThe Code is as follows:
Def gen ():
For I in range (4 ):
Yield I
It can also be written as a Generator Expression (Generator Expression ):
Copy codeThe Code is as follows:
G = (x for x in range (4 ))
Generator expressions are a simple way to compile generators. For more information, see.
Table Derivation
List comprehension is a quick way to generate tables. Its syntax is simple and has great practical value.
Suppose we generate table L:
Copy codeThe Code is as follows:
L = []
For x in range (10 ):
L. append (x ** 2)
The preceding table L is generated, but there is actually a quick writing method, that is, the table deduction method:
Copy codeThe Code is as follows:
L = [x ** 2 for x in range (10)]
This is similar to the generator expression, except that brackets are used.
(The mechanism of table derivation is actually to use circular objects. If you are interested, you can refer to them .)
What will be generated by table derivation below the exercise?
Copy codeThe Code is as follows:
Xl = [1, 3, 5]
Yl = [9, 12, 13]
L = [x ** 2 for (x, y) in zip (xl, yl) if y> 10]
Summary
Loop object
Generator
Table Derivation
How does one cyclically assign values to object attributes in python ??
Are you an abstract master?
For Loop operations in Python
The variable will not be replaced.
R' "C: \ Users \ Administrator \ Documents ents \ Visual Studio 2008 \ Projects \ videoDetection \ Debug \ videoDetection.exe"-I L0-w 1-l 20-t 30'
Changed:
R' "C: \ Users \ Administrator \ Documents ents \ Visual Studio 2008 \ Projects \ videoDetection \ Debug \ videoDetection.exe"-I % s-w 1-l 20-t 30' % L0