Python Builder (note)

Source: Internet
Author: User

Python Builder is a bit difficult to express its concept in words, so here are a few pieces of code to explain ~

Generator: Any function that contains a yield statement is called a generator;

The generator is an iterator defined by a common function syntax.


Def test2 (): Print 9 Print 8 yield 7test2 ()

The above code does not return anything, because the code encounters the yield function to pause (or freeze), and this pause also affects the yield above the two print.


Def test2 (): Print 9 Print 8 yield 7t2 = Test2 () t2.next ()-------------------------------------------------------- ------------------------[[email protected] tmp]#./1.py98

By using the next method, yield is activated so that two print statements that are paused by yield are output.

But what is the 7 after yield? It looks like it's going to be output, but how come there's no output?

This is because, using the next method, just let the frozen content continue to execute, and yield is only activated, and it does not output the value behind it, but save it.


The yield has already saved the value here, how to output it ~

Def test2 (): Print 9 Print 8 yield 7t2 = Test2 () print t2.next ()-------------------------------------------------- ------------------------------[[email protected] tmp]#./1.py987

Right, just need a print, the above two pieces of code can be seen, t2.next () just pause to continue (activate), and save the yield value, and the yield statement does not output the value, so you have to print out the value with print.

In this way, the concept of a general yield is clear, and we understand the generator of the two-level nested list below.

def FLT (NES): For I in Nes:for j in I:yield jnested = [[1, 2], [3, 4], [5]]for num in Flt (nested): Print Numprint "---------" Print List (flt (nested))---------------------------------------------------------------- ----------------[[email protected] tmp]#/1.py 12345---------[1, 2, 3, 4, 5]

Because the generator has a concept of "the generator is an iterator defined by a common function syntax ." So the generator is an iterator and can be iterated.

the two-layer for loop of the function Flt () is clear, which is to take elements from a two-level nested table. The code above uses an iterative for loop to take an element, take an element , and manipulate an element.

So the yield function is that the yield statement is the sovereign of the entire function, and all code, regardless of what you return or print before the yield code, will be paused until activation occurs.


Python Builder (note)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.