Python cultivation 7----iterator

Source: Internet
Author: User

Iterators

--------------------------------------------------------------------------------

Charging Tip

1.yield---> custom functions as long as Y writes a point, it means that the function is already a generator.

Yield--->m=yield 5 131 can be assigned as an expression, and the value of M is 131,5 is the value returned by the def-defined Function.

Yield--->next () function must be executed after The. __next__ () method is called, until the return value Stopiteration stopped

2.send () and Next ()

Send () is basically the same as next (), but send (*) can enter the expression value of yield, and next () cannot pass a specific value, only pass none in

Friendly Reminder: When the first call, use the next () statement or send (None), you cannot use Send to send a value other than None, otherwise it will be wrong, because there is no yield statement to receive this value

3. Three-dimensional expression----

def F ()

Return "+" if n>0 else "-" understanding satisfies if return otherwise "-"

1 Sum (x * * 2 for X in xrange (4)) to achieve "a-z"  Square summation      

--------------------------------------------------------------------------------

1. Iterator protocol

An iterator protocol is an object that must provide a next method that either returns the next item in the iteration, or causes a Stopiteration exception to terminate the iteration (only backward cannot go forward)

2.python ' in for loop mechanism

1. Turn them into an iterative object by calling The. __iter__ () method First.

2. The For loop then calls the __next__ method to take the value

The 3.for loop captures the stopiteration exception to terminate the iteration

3. The generator

1. Generator Concept: Automatic implementation of the iterator protocol (other data types need to call their own built-in __iter__ method), so the generator is an iterative object

1.1 Ways to provide generators:

1. Generator Function--custom function, Use yield statement to return results, yield statement return one result at a time, in the middle of each result, suspend the state of the function so that the next time it left the place to continue execution

2. Generator expression--like A list derivation, but the generator returns an object that produces results on demand, rather than building a list of results at a time

2. Generator functions

def F ():

Yield------default is

Advantage: the generator uses the yield statement to return a Value. The yield statement suspends the state of the generator function, preserving enough information so that it can continue to execute from where it left off.

Advantage One: the benefit of the generator is a deferred calculation that returns one result at a Time. That is, it does not produce all the results at once, which is useful for large data processing.

Advantage Two: Generator can also effectively improve code readability

Note: the generator can only be traversed once (hen life can only be a certain number of eggs, the end of the Dead)

  

    

Python cultivation 7----iterator

Related Article

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.