python-iterators and generators

Source: Internet
Author: User
Tags generator

Reference:

http://python.jobbole.com/81911/

Http://www.cnblogs.com/kaituorensheng/p/3826911.html

One, iterator

Iterators are a way to access the elements of a collection. The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished. Iterators can only move forward without going backwards, but that's fine, because people seldom retreat in the middle of an iteration.

1, the advantages of iterators

For primitive data structures that support random access (such as tuple, list), there is no advantage over the index access of the iterator and the classic for loop, but the index value is lost (you can use the built-in function enumerate () to retrieve the index value). But for data structures that cannot be accessed randomly (such as set), iterators are the only way to access elements, and iterators are classes that contain the next function.

In addition, one of the great advantages of iterators is that they do not require that all elements in the entire iteration be prepared in advance. An iterator computes an element only when it iterates over it, and before or after that, the element may not exist or be destroyed. This feature makes it particularly useful for traversing large or infinite collections, such as several G files, or Fibonacci sequences, and so on.

The greater credit for iterators is the provision of an interface for a unified access collection, as long as the __iter__ () method object is defined, it can be accessed using an iterator, and the Python processing iterator is out of bounds to throw the stopiteration exception.

There are two basic methods for iterators

    • Next method: Returns the next element of the iterator
    • __iter__ method: Returns the Iterator object itself

Here's an example of how to use an iterator to generate Fibonacci numbers:

#This method takes up memory resourcesdefFab (max): N, a, b= 0, 0, 1L=[]     whileN <Max:a, b= B, A +b l.append (b) n= n + 1returnL#you can use iterators to solve the problem of complex spaceclassFab (object):def __init__(self, max): Self.max=Max SELF.N, SELF.A, self.b= 0, 0, 1def __iter__(self):return SelfdefNext (self):ifSELF.N <Self.max:r=self.b self.a, self.b= self.b, SELF.A +self.b SELF.N= SELF.N + 1returnRRaisestopiteration ()#Execute Code fromIteratorsImportFab forKinchFab (5):    Print(k)#use the built-in factory function iter (iterable) to get an iterator object: forIinchiter (FAB):Print(i)

Second, generator

The function with yield is called generator (generator function) in Python, the list of things that the generator expression can do is basically handled, but the list parsing compares the memory when the sequence that needs to be processed is relatively large.

def Fab (max):     = 0, 0, 1     while n < Max:        #  stops each time it executes to yield, returns the current value of the variable after yield        yield  b        = b, A + b        = n + 1

 Simply put, the function of yield is to turn a function into a generator, the function with yield is no longer a normal function, the Python interpreter treats it as a generator, the call FAB (5) does not execute the FAB function, but returns an I Terable Object ! When the For loop executes, each loop executes the code inside the FAB function, and when it executes to yield B, the FAB function returns an iteration value , and the next iteration, the code proceeds from the next statement of Yield B, and the local variable of the function It looks exactly the same before the last interrupt execution, so the function continues execution until yield is encountered again. It looks as if a function was interrupted several times by yield during normal execution, and each break returns the current iteration value through yield.

In a generator, if there is no return, the default execution to the function is complete; if return is encountered, if return is executed, then the stopiteration terminates iteration is thrown directly .

def G2 ():     yield ' a '    return    yield ' b ' g=G2 ()# The program stops at the position after the yield ' a ' statement is executed.  Next (g)  # The program finds that the next statement is return,#  so throws a Stopiteration exception, So the yield ' B ' statement will never be executed. Next (g)  

Methods supported by the generator

defg4 ():yield1yield2yield3Print(dir (G4 ())) ['__class__','__del__','__delattr__','__dir__',  '__doc__','__eq__','__format__','__ge__','__getattribute__', '__gt__','__hash__','__init__','__iter__','__le__','__lt__', '__name__','__ne__','__new__','__next__','__qualname__', '__reduce__','__reduce_ex__','__repr__','__setattr__','__sizeof__',  '__str__','__subclasshook__','Close','Gi_code','Gi_frame', 'gi_running','Gi_yieldfrom','Send','Throw']

Close ()

Close the generator function manually, and the subsequent call will return the stopiteration exception directly

def g4 ():     yield 1    yield 2    yield 3g=G4 () Next (g) G.close () Next (g)#  after closing, yield 2 and yield 3 statements will no longer function "<stdin>" in < module>stopiteration

Send ()

The biggest feature of the generator function is the ability to accept an externally passed-in variable and return it based on the contents of the variable. This is the most difficult place to understand the generator function, but also the most important place, the implementation of the later I will talk about the process of it all depend on it.

    • by G.send (None) or Next (g), you can start the generator function and execute to the point where the first yield statement ends. At this point, the yield statement is executed, but receive is not assigned a value. Yield value outputs the initial value 0 Note: You can only send (None) when you start the generator function, and you will get an error message if you try to enter a different value.
    • by G.send (' AAA '), the AAA is passed in and assigned to receive, then the value of value is computed and returned to the while header, and the yield value statement is stopped. The yield value now outputs "GOT:AAA" and then hangs.
    • Through G.send (3), the 2nd step is repeated and the result is "Got:3″
    • When we g.send (' End '), the program executes a break and then rolls out the loop, and finally the entire function is finished, so we get the stopiteration exception.
defGen (): Value=0 whiletrue:receive=yieldvalueifreceive=='End':             Breakvalue='Got:%s'%Receiveg=Gen ()Print(G.send (None))Print(G.send ('AAA'))Print(G.send (3))Print(G.send ('End'))

Throw ()

Used to feed an exception to the generator function, which can end a system-defined exception, or a custom exception. Throw () runs directly out of the exception and ends the program, either consumes a yield, or goes directly to the end of the program without the next yield.

    • Print (Next (g)): The normal value is output and stays before yield ' normal value 2 '.
    • Because G.throw (valueerror) is executed, all subsequent try statements are skipped, which means that the yield ' normal value 2 ' will not be executed, and then go into the except statement and print out the We got valueerror here. It then enters the while statement part again, consumes a yield, and outputs the normal value.
    • Print (Next (g)) executes the yield ' normal value 2 ' statement and stays where it was after the statement was executed.
    • G.throw (TypeError): Will jump out of the try statement so that print (' here ') will not be executed, then execute the break statement, jump out of the while loop, and then reach the end of the program, so run out of the stopiteration exception.
defGen (): whileTrue:Try:            yield 'Normal value'            yield 'Normal value 2'            Print(' Here')        exceptValueError:Print('we got ValueError here')        exceptTypeError: Breakg=Gen ()Print(Next (g))Print(G.throw (valueerror))Print(Next (g))Print(G.throw (TypeError))

Summarize:

According to duck model theory, a generator is an iterator that can be iterated using for.

The first time you execute next (generator), the program suspends after the yield statement is executed, and all parameters and states are saved. Once again, when you execute next (generator), it is executed from the suspended state. The loop ends when the end of the program is encountered or when Stopiteration is encountered.

Parameters can be passed in by Generator.send (ARG), which is the model of the co-process.

You can pass an exception by Generator.throw (Exception). The throw statement consumes a yield. You can manually turn off the generator by Generator.close ().

Next () equivalent to send (None)

python-iterators and generators

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.