Python iterator, generator and decorator, and python generator Decoration
1. Principles and usage of the iterator:
1> principle:
An iterator is a way to access a set element. The iterator object is accessed from the first element of the set until all elements are accessed. The iterator can only move forward and not backward, but that's nothing. People seldom step back on the iteration process. In addition, the major advantage of the iterator is that they do not need to prepare all the elements in the entire iteration process in advance; the iterator calculates the element only when it iterates to an element. Before or after this, the element may not exist or be destroyed; this feature makes it especially suitable for Traversing large or infinite sets, such as several GB of files;
Features:
(1) visitors do not need the internal structure of the relational iterator. They only need to use the next () method to retrieve the next content;
(2) A value in the set cannot be randomly accessed, but can only be accessed from start to end in sequence;
(3) Half of the access requests cannot be returned;
(4) easy to collect large data sets and save memory;
2> Generate an iterator: 3. Use _ next _ () to view the iterator;
2. generator ):
Definition: When a function is called, an iterator is returned. This function is called a generator. If the function contains the yield syntax, the function will become a generator;
Yield:
This can interrupt the function and save the interrupt status. After the interruption, the code can continue to be executed. After a while, you can call the function again to execute the function from the next yield sentence;
1> generator generation:
2> use yield to Implement Asynchronous concurrency in multiple threads:
2. decorator: expands new functions for implemented functions;
1> decorator without parameters;
2> A parameter-like decoration device;
3> decorator:
4> implement a complex decorator with parameters: a function is decorated by multiple decorator;
Process Analysis: