A detailed introduction to Python iterators and generators

Source: Internet
Author: User
What kind of thing is an iterator?

Let's start with an iteration, which is one thing that repeats many times, like a for loop.

The For loop can iterate over all the objects that have the ITER method, so what is the Iter method?

Whether an object can be iterated depends on whether the object has a ITER method, calls the object's Iter method, returns an iterator that must have the next method, and when the next method of the iterator is called, the iterator returns its next value. When no value in the iterator can be returned, an exception named Stopiteration is thrown back, stopping the iteration.

Iterators also have a very important feature, that is irreversible, can only move forward, can not retreat.

The For loop works like this, and the For loop loops through an object, invokes the object's Iter method, gets the iterator, and then calls the next method of the iterator to get each value contained in the iterator.

Two. What is the difference between a list and an iterator? How can a basic iterator be implemented?

iterators work by calculating a value when used to get a value, and the list is to get all the values once, and if there are many values, it takes up a lot of memory.

How do you make your objects iterate when you create an object yourself?

Class Test_class:

def init (self,start_num,stop_num):

Self.start_num = start_num

Self.stop_num = Stop_num

Def next (self):

If Self.start_num < self.stop_num:

Self.start_num + = 1

Return Self.start_num

def iter (self):

return self

Test_obj = Test_class (0,3)

Print Test_obj.next ()

>>>1

Print Test_obj.next ()

>>>2

Print Test_obj.next ()

>>>3

Three. What is a generator?

As a personal understanding, the generator is a special iterative object, unlike other iterative objects, where other iterative objects need to call the Iter method, return an iterator object, and then execute the next method through the iterator object to get the value in the iterator. But the generator can be iterated directly without the need to execute the ITER method.

In Python, the generator has two forms of expression:

Function Builder: That is, the generator defined in the regular function, the return value of the statement is no longer returned with return, but instead uses the yield keyword to return a result each time, a function can not have more than one return, but can have multiple yield, Each yield in the function returns a result, with each yield being executed, the execution state of the function will be ' suspended ' as paused, and the next time the function is called, it will continue to execute from the last pending position.

Here is an example of a function builder:

The following example verifies the two characteristics of yield, the first is that a function can yield multiple values, have multiple yields, and the other is the hang feature of the function generator.

Def func1 ():

Yield 1

Print "first yield execution complete ~"

Yield 2

Print "second yield execution complete ~"

Yield 3

Print "Third yield execution complete ~"

For I in Func1 ():

Print I

>>>1

First yield execution Complete ~

2

Second yield execution Complete ~

3

Completion of the third yield execution ~

Builder expression: A method similar to a list derivation is used, but the returned object is no longer a list, but rather an object (builder) that can produce results on demand.

Example 1:

For I in (I-I in range (10000)):

Print I

(I-I in range (5)) This is the generator expression.

(I for I in range (10000)) = def test (): For I in range (10000): Yield I

These two kinds of writing function is the same, but it is written differently, one is the generator expression, and the other is a function generator.

Do you think that this generator expression and the list derivation look very similar, the difference is that the list derivation is the use of [] brackets, and the generator expression uses () parentheses?

It is true that the syntax between them is really only one parenthesis, but the generator expression is more memory-efficient.

About the generator, roughly speaking almost, and finally a summary:

The generator definition method is exactly the same as the normal function, where the generator uses yield to return a value, and the function returns a value using return.

In Python, the generator automatically implements the iterative protocol, returning a stopiteration exception when no value can be returned.

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 where it left off.

The following example is a comparison of the efficiency of the list derivation and the generator expression execution, and the small partner of interest can try it out on their own computer.

#列表解析

SUM ([I for I in Range (100000000)]) #内存占用大, the machine is easy to die

#生成器表达式

SUM (I for I in range (100000000)) #几乎不占内存

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.