Generator of python--function

Source: Internet
Author: User
Tags generator

1 What the generator function means

A generator is a function that returns an object that can be iterated, and it is a special iterator, but the level of abstraction of the iterator is higher and more complex requires many methods to be implemented. The generator is simple to use compared to iterators.

2 How generators are created    2.1 Generator Expression

    Modify the list derivation [] to (), as

 for inch if i% 2 = = 0)

The Code execution interface is as follows:

    

 2.2 Generator functions

    (1) using the yield keyword in a generic function allows you to implement a simplest generator, where the function becomes a generator function. In simple terms, the generator function is the function

The function that contains the yield statement.

yield Syntax : YielD [return value] ([] indicates an optional parameter).

The effect of yield : yield can be used to block the current function execution. When the next () (or. __next__ ()) function is used, the function continues to execute, and the value following the yield is used as the next () function

The return value, when executed to the next yeild, and Will Suspended pending.

the difference between yield and return : Yield saves the execution state of the current function, and after it returns, the function goes back to the previously saved state, and return returns, the function

The state terminates.
(2) Code example

1 defmylist ():2      forIinchRange (3):3         Print("the first%s element of the list:"% i, end="")4         yieldI5 6 7ml = MyList ()#Create a generator8 Print(Next (ML))9 Print(Next (ML))Ten Print(Next (ML)) One Print(Next (ML))#when the generator has traversed, the traversal will result in an error.

    Code Execution Order:

      

3 Traversal of the generator

3.1 Next () or. __next__ () traversal,   The code is as follows

  

1 #Mode 12ml = (i forIinchRange (1, 10)ifI% 2 = =0)3 Print("next () way to traverse the result:", end="")4 Print(ML.__next__(), end=" ,")5 Print(ML.__next__(), end=" ,")6 Print(ML.__next__(), end=" ,")7 Print(ML.__next__())8 9 #Mode 2TenML2 = (i forIinchRange (1, 10)ifI% 2 = =0) One Print(". __next__ () method traversal result:", end="") A Print(Next (ML2), end=" ,")#next () equals generator. __next__ () - Print(Next (ML2), end=" ,") - Print(Next (ML2), end=" ,") the Print(Next (ML2))

The result of the above code execution is as follows:
  

3.2  for in mode traversal, code as follows,

1  for inch if i% 2 = = 0)2print("For in"iterates over theresult; ", end="")3 for in ml:4      Print(i, end="")

The result of the above code execution is:

    


4 Closing the generator
  command : Generator name. Close (), such as Ml.close ().

Note : The generator will only traverse once, and when the generator is closed or traversed again, the stopiteration exception prompt will be thrown if subsequent calls are repeated. The next time you want to continue   the re-traversal, you must first recreate the builder.

Generator of python--function

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.