The definition and use of yeild in python

零到壹度
Release: 2018-03-29 15:24:21
Original
5996 people have browsed it


yeild is simply a generator. A generator is a function that remembers the last time it returned position in the function body. The second (or nth) call to a generator function jumps to the middle of the function, leaving all local variables unchanged from the previous call.

The generator is a function

All parameters of the function will be retained

The second time The parameters used when calling this function are the ones left from the previous time

The generator also "remembers" its data state. Also remember its location in the flow control construct.

Generator operating mechanism

##When you ask the generator to generate a number, the generator will execute until the yeild statement appears. The generator will give you the parameters of yeild, and then the generator will not Run down. When you ask it for the next number, it will continue running from the last state until the yield statement appears, give you the parameters, then stop, and repeat until the function exits

In order to understand the mechanism of yield, we need to understand what a generator is. Before that, let’s introduce iterators.

Iterables

When you create a list, you can get it one by one. This kind of list is called Iteration:

<span style="font-size: 16px;">>>> mylist = [1, 2, 3]<br>>>> for i in mylist:<br>...    print(i)<br>1<br>2<br>3<br></span>
Copy after login

Mylist is an iterator. When you understand it as a list, it is iterable:

<span style="font-size: 16px;">>>> mylist = [x*x for x in range(3)]<br>>>> for i in mylist:<br>...    print(i)<br>0<br>1<br>4<br></span>
Copy after login


Anything that can be read iteratively using for in is an iterative container, such as lists, strings, files. These iterators are very convenient, because you can take as much as you want, but you have to Store all values, many of which are completely unnecessary to keep in memory every time.

Generators

Generators(生成器)也是可迭代的,但是你每次只能迭代它们一次,因为不是所有的迭代器都被一直存储在内存中的,他们临时产生这些值:

<span style="font-size: 16px;">>>> mygenerator = (x*x for x in range(3))<br>>>> for i in mygenerator:<br>...    print(i)<br>0<br>1<br>4<br></span>
Copy after login


生成器几乎和迭代器是相同的,除了符号[]变为()。但是你无法用两次,因为他们只生成一次:他们生成0然后丢弃,继续统计1,接着是4,一个接着一个。

Yield

Yield的用法有点像return,除了它返回的是一个生成器,例如:

<span style="font-size: 16px;">>>> def createGenerator():<br>...    mylist = range(3)<br>...    for i in mylist:<br>...        yield i*i<br>...<br>>>> mygenerator = createGenerator() # create a generator<br>>>> print(mygenerator) # mygenerator is an object!<br><generator object createGenerator at 0xb7555c34><br>>>> for i in mygenerator:<br>...     print(i)<br>0<br>1<br>4<br></span>
Copy after login


上面的例子几乎非常积累,但是它很好的阐释了yield的用法,我们可以知道createGenerator()生成的是一个生成器。


为了掌握yield的精髓,你一定要理解它的要点:当你调用这个函数的时候,你写在这个函数中的代码并没有真正的运行。这个函数仅仅只是返回一个生成器对象。有点过于奇技淫巧:-)


然后,你的代码会在每次for使用生成器的时候run起来。


现在是解释最难的地方:

当你的for第一次调用函数的时候,它生成一个生成器,并且在你的函数中运行该循环,知道它生成第一个值。然后每次调用都会运行循环并且返回下一个值,知道没有值返回为止。该生成器背认为是空的一旦该函数运行但是不再刀刀yield。之所以如此是因为该循环已经到达终点,或者是因为你再也不满足“if/else”的条件.

Related recommendations:

A brief analysis of python yeild

A brief analysis of python yeild Analysis

The above is the detailed content of The definition and use of yeild in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!