Python --- Advanced features (generator, iterator, closure)

Builder

What generator?
In Python, while the side computing cycle mechanism, called generator: Generator.
When do I need to use the generator?
Generally we do not need to use the generator only when we need to use because of performance limitations, such as we use the python to read a
th file 10g, 10g if you to load a file into memory, then processing (read method), memory will certainly overflow; here if you can
use the generator to write cross-processing, such as using (readline and readlines) will continue to read and can be recycled process, so that you can save a lot of memory space?.
how to create generator
a first method: a listing of formula rewritten. [] Into ()
The first method: yield keyword.
How to print each element generator do?
Through the for loop, in order to calculate and generate each element.
If you want to print out one by one, it is possible to obtain the function generator by a return value next ().
What are the characteristics of the generator is?

  1. Save memory
  2. When a call to the next iteration, the parameters used are the first under the retention, that is
    to say, the entire reserved in all the parameters of the function call is the first call, rather than a new
    created.

Practice on the producer lesson: Applications Builder: chatbot
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
on the generator class exercise: Applications Builder: producer - consumer model
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
based on producers and consumers in stock of
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
the generator class exercise: Builder application case: averaging
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Iterator

定义
迭代是访问容器元素的一种方式。迭代器是一个可以记住遍历的位置的对象。
迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。
可迭代对象:可以直接作用于for循环的对象(如何判断是否可以迭代?)
一类是集合数据类型,如list, tuple,dict, set,str等;
一类是generator,包括生成器和带yield的generator function。
可以被next()函数调用并不断返回下一个值的对象称为迭代器:Iterator。
生成器都是Iterator对象,但list、dict、str虽然是Iterable,却不是Iterator。
把list、dict、str等Iterable变成Iterator可以使用iter()函数

Here Insert Picture Description

可迭代对象
Here Insert Picture Description
Here Insert Picture Description
调用next方法
Here Insert Picture Description
Here Insert Picture Description

String loop to achieve for
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
how the judge is not iterables
Here Insert Picture Description
Here Insert Picture Description
determine whether the file object iterable
Here Insert Picture Description
Here Insert Picture Description
closure
function references?
What is a closure?
The concept of closures is that when we define a function within a function, this function uses the internal temporary variables for external functions, and external function returns a reference value is an internal function, we call closure.
Closures then understand? Internal function of the external function scope in reference to a variable (non-global variables), called for the closure internal function.
nonlocal keyword? local variables explicitly specified variable is not the closure of
a popular scene closure is the decorator.
Examples of small
Here Insert Picture Description
Note: the function line and variables a, b constitute closures. Creating closures, we line_conf by the parameters a, b illustrate the values of these two variables, and
so we determined the final form of a function (y = x + 1 and y = 4x + 5).
Advantages: closure also has the role of improving the code reusability.

Guess you like

Origin blog.csdn.net/weixin_43592835/article/details/90908077