Beginner Python Builder

Source: Internet
Author: User

Generator (generator) concept

Instead of saving the result in a series, the generator saves the state of the generator and returns a value each time it is iterated until the end of the stopiteration exception is encountered.

Builder syntax

Generator expression: pass-through list parsing syntax, just replace the list parsing [] with ()
The list of things that the builder expression can do is basically handled, but the list parsing compares the memory when the sequence that needs to be processed is relatively large.

 >>> gen = (x**2 for  x in  Range ( 5 >>> gen  <generator Object <genexpr> at 0x0000000002fb7b40>>>> for  g in   gen: ...  print  (g, End= '  -  '  < span> -  '  

Generator function: If the yield keyword appears in the function, then the function is no longer a normal function, but a generator function.

But the generator function can produce a wireless sequence so that the list has no way of handling it at all.

The effect of yield is to turn a function into a generator, and the function with yield is no longer a normal function, and the Python interpreter treats it as a generator.

Here is a generator function that can produce an infinite number of odd numbers:

def Odd ():    n=1     while True:         yield n        N+=2== 0 for in odd_num:     if  Break    Print (o)     +=1

yield and return

In a generator, if there is no return, the default execution is returned to the stopiteration when the function is complete;

def G1 ():      ... yield 1 ... >>> g=G1 ()>>> Next (g)    # when the first call to Next (g) is suspended after the yield statement is executed, the program does not end at this point. 1>>> Next (g)    # The program tries to start execution from the next statement in the yield statement, and finds that it has reached the end, so it throws a Stopiteration exception.  Traceback (most recent  ):'<stdin>'in <module>stopiteration>>>

If a return is encountered, the Stopiteration abort iteration is thrown directly if the return is executed during execution.

>>>defG2 (): ...yield 'a'...     return...     yield 'b'...>>> g=G2 ()>>> Next (g)#the position at which the program stops after the yield ' a ' statement is executed. 'a'>>> Next (g)#The program finds that the next statement is return, so throws a Stopiteration exception so that the yield ' B ' statement will never execute. Traceback (most recent): File"<stdin>", Line 1,inch<module>stopiteration

If a value is returned after return, then this value is the description of the stopiteration exception, not the return value of the program.

The generator has no way to return a value using return.

>>>defG3 (): ...yield 'Hello'...     return ' World'...>>> g=G3 ()>>>Next (g)'Hello'>>>Next (g) Traceback (most recent): File"<stdin>", Line 1,inch<module>Stopiteration:world

the methods supported by the generator can be seen in more detail in http://python.jobbole.com/81911/

Beginner Python Builder

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.