Home > Backend Development > Python Tutorial > Detailed introduction to python iterators and generators

Detailed introduction to python iterators and generators

高洛峰
Release: 2017-03-22 10:00:10
Original
1296 people have browsed it

1. What is an iterator?

Let’s first talk about what iteration is. Iteration is to repeat one thing many times, such as for loop.

The for loop can iterate over all objects that have iter methods. So what is the iter method?

Whether an object is iterable or not depends on whether the object has an iter method. Calling the object's iter method will return an iterator. This iterator must have a next method. When the next method of this iterator is called, the iterator returns its next value. When there is no value in the iterator to return, an exception named StopIteration is thrown to stop iteration.

Another very important feature of iterators is that they are irreversible and can only move forward, not backward.

This is how the for loop works. When the for loop loops through an object, it will call the iter method of the object to get the iterator, and then call the next method of the iterator to get the iterator. Every value included.

2. What is the difference between lists and iterators? How can I implement a basic iterator?

The way an iterator works is to calculate a value and get a value when using it, while a list gets all the values ​​at once. If there are many values, it will take up a lot of memory.

When you create an object yourself, how do you make your object iterable?

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

3. What is a generator?

My personal understanding is that the generator is a special iterable object. It is different from other iterable objects. That is, other iterable objects need to call the iter method and return an iterator object. , and then execute the next method through the iterator object to obtain the value in the iterator, but the generator can be iterated directly without executing the iter method.

In python, generators have two expression forms:

Function Generator: that is, literally, in a regular function For the defined generator, the return value of the statement no longer uses return, but uses the yield keyword to return one result each time. There cannot be multiple returns in a function, but there can be multiple yields. Each of the functions in the function Yield will return a result. Every time a yield is executed, the function's execution status will be 'suspended', which can be understood as suspended. The next time you continue to call this function, it will continue from the last suspended position. Execute downward.

The following is an example of a functional generator:

The following example verifies the two characteristics of yield. The first is that a function can yield multiple values ​​and there are multiple yields. Another one is the suspension feature of functional generators.

def func1():

yield 1

print "The first yield execution is completed~"

yield 2

print "The execution of the second yield is completed~"

yield 3

print "The execution of the third yield is completed~"

for i in func1():

print i

>>>1

The first yield execution is completed~

2

The second yield execution is completed~

3

The third yield execution is completed~

GeneratorExpression: Use a method similar to list comprehension, but return the object It is no longer a list, but an object (generator) that can generate results on demand.

Example 1:

for i in (i for i in range(10000)):

PRINT I

## (I for 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 writing methods have the same effect , just written in different ways, one is a generator expression, and the other is a functional generator.

Do you think that this kind of generator expression looks very similar to a list comprehension. The difference is that the list comprehension uses [] square brackets, while the generator expression uses () brackets. brackets?

The fact is that there is only one parenthesis difference in the syntax between them, but generator expressions save more memory space.

About the generator, that’s about it. Let’s give a summary:

The definition method of the generator is exactly the same as that of the ordinary function. The difference is that the generator uses yield to return A value, the function uses return to return a value.

In python, the generator will automatically implement the iteration protocol and return a StopIteration exception when there is no value to return.

The generator uses the yield statement to return a value. The yield statement suspends the state of the generator function, retaining enough information to later resume execution from where it left off.

The following example is a comparison of the execution efficiency of list comprehensions and generator expressions. Interested friends can try it out on their own computers.

#List parsing

sum([i for i in range(100000000)])#The memory usage is large and the machine is easy to get stuck

#Generator expression

sum(i for i in range(100000000))#Almost no memory occupied

The above is the detailed content of Detailed introduction to python iterators and generators. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template