Python iterator, generator

Source: Internet
Author: User
Tags error handling for in range generator generator

What do you mean by Fall generation :

The data within a data set can be removed one by one, and it is called a fall generation.

Iterator protocol:

A requirement that can be met by a fall is called an iterative protocol, and an iterative object must provide a next method that either returns the next item in the fall, or causes a Stopiteration exception to terminate the Fall generation (the fall generation can only go backwards, but not forward)

For loop in Python:

The nature of the For loop is to follow the iterator protocol to access the object, the For loop can traverse (string, list, progenitor, dictionary, collection, file object) These objects are non-iterative objects, but in the For loop, call their internal-iter-method, make them into an iterative object, The For loop then calls the-next-method of the iteration object to fetch the value, and the for loop catches the stoplteration exception to terminate the fall generation.

 #  while loop simulation for loop  diedai_1 = L.__iter__   ()  while   try : #   error handling mechanism  print  (diedai.< Span style= "COLOR: #800080" >__next__   ())   Except  stopiteration:#   Automatically captures stopiteration errors to prevent error  print  ( "  Fall generation complete   " )  break  

The For loop provides us with a method that does not depend on the index value, which provides a uniform way to traverse all objects, based on the iterator protocol, before iterating, calling the object's __iter__ method to convert it to an iterator, and then using the iterator protocol to implement the Loop access. This allows all objects to traverse through the for loop.

What is an iterator:

An object that has the __iter__ method and the __next__ method is called an iterator.

L = [1,2,3,4]l_iter= L.__iter__() Item= L_iter.__next__()#take one value at a timePrintItem (item)= L_iter.__next__()PrintItem (item)= L_iter.__next__()PrintItem (item)= L_iter.__next__()PrintItem (item)= L_iter.__next__()#no value for error exception stopiterationPrint(item)

Advantages of iterators: Provide a way to take a value that does not depend on the index.

Lazy calculation, saving memory.

The disadvantage of iterators: it is not as easy to take a value as an index.

One-time, can only go back, can not forward.

Unable to get length.

Generator:

What is a generator: it can be understood as a type of data that automatically implements the iterator protocol (other data types need to call their own built-in __iter__ method), so the generator is an iterative object.

The generators available in Python:

1. Generator function: General function definition, however, returns the result using the yield statement instead of the return statement. The yield statement returns one result at a time, in the middle of each result, suspends the state of the function so that the next time it leaves, it resumes execution.

2. Generator expression: Similar to list derivation, however, the generator returns an object that produces results on demand, rather than building a list of results at a time

Generator generator:

Essence: iterators (so we have the __iter__ method and the __next__ method, we do not need to implement)

Features: Lazy operation, developer customization

Generator functions:


A function that contains the yield keyword is a generator function. Yield can be the return value from the function, but yield is different from the execution of Return,return means the end of the program, and the Call builder function does not get the exact value returned, but instead gets an iterative object. Each time you get the value of this iterative object, you can push the execution of the function to get a new return value. Until the end of the function execution.

The function that contains the yield keyword is called the generator functiondefgenrator_fun1 (): a=1Print("The A variable is now defined")    yielda B= 2Print("and now we've defined the B variable.")    yieldbg1=genrator_fun1 ()Print(G1)Print(Next (G1))Print(Next (G1))

What is the benefit of the generator, do not generate too much data in memory at once, if the production of a batch of 2 billion pieces of clothing, a batch of take, you can use the generator function to represent.

defProduce (): forIinchRange (20000):        yield "produced the first piece of%s clothes"%Iproduct_g=Produce ()Print(Product_g.__next__())#first piece of clothingPrint(Product_g.__next__())#second piecePrint(Product_g.__next__())#Third piecenum =0 forIinchProduct_g:#Take 30 pieces    Print(i) Num+ = 1ifnum = = 30:         Break

More applications

Import Timedeftail (filename): F=open (filename) f.seek (0,2)#count from the end of the file     whileTrue:line= F.readline ()#read new lines of text in a file        if  notLine:time.sleep (0.1)            Continue        yieldTail_g= Tail ("tmp")#"tmp" file name forLineinchTail_g:Print(line)
Generator Listener File input
#simulated average salarydefaverage (): Total=0 Day=0 Average=None whileTrue:term=yieldAverage#equivalent to a return of a averageTotal + = term#The total wage equals the sum of all the wages of these daysDay + = 1#The day is over and one is added.Average = total/Dayavg=average () Next (avg)#here is the activation generator, avg.send () What value does not pass, and next effect the samePrint(Avg.send (10))#send has two functions, passes the value and looks for the next yield (next)Print(Avg.send (20))
Calculate moving averages

The generator wants to run, it must be activated with the next () method at the time of invocation, then it can be used without activating the direct fetch, we need to add an adorner to the generator to solve the problem.

defInit (func):defInner (*args,**Kwargs): G= Func (*args,**kwargs)#the function average incoming func () with G to accept the return valueNext (g)#the generator is activated here        returngreturnInner@initdefaverage (): Total=0 Day=0 Average=None whileTrue:term=yieldaverage Total+= Term Day+ = 1Average= total/Dayavg=average ()#Next (AVG), using the adorner to perform the next methodPrint(Avg.send (10))Print(Avg.send (100))Print(Avg.send (150))
Decorator Decorator Builder

Yield from

defgen1 (): forCinch "AB":        yieldC forIinchRange (3):        yieldIPrint(List (Gen1 ()))#[' A ', ' B ', 0, 1, 2]defGen2 ():yield  from "AB"    yield  fromRange (3)Print(List (Gen2 ()))#[' A ', ' B ', 0, 1, 2]

List derivation and generator parsing

Egg_list = [" egg%s" for in range]print(egg_list) # List-parsed Laomuji = (" egg%s" for in range) Print (Laomuji) # The builder expression, which is to replace the square brackets [] of the list parsing with the ()

A list-parsed [] switch to () is a generator expression, and list parsing and builder expressions are a convenient way of programming, except that generator expressions are more memory-efficient than list-parsing. Python not only uses the iterator protocol to make the For loop more generic, most built-in functions, but also access objects using an iterator protocol.

Summarize:

An iterative object: Owning the __iter__ () method, which is characterized by lazy computation, saving memory.

Iterators: Iterator has __iter__ methods and __next__ methods

Generator: Generator Essence even if the iterator features: Lazy calculation, return one result at a time, which is very useful for a lot of data processing.

Python iterator, generator

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.