Python iterator and generator

Source: Internet
Author: User
This article introduces the python iterator and generator in detail. 1. what is an iterator?

Let's talk about iteration first. iteration is a process that repeats many times, for example, for loop.

The for loop can iterate on all objects with iter methods. what is the iter method?

Whether an object can be iterated depends on whether the object has an iter method. when the object's iter method is called, an iterator is returned. this iterator must have the next method, when the next method of the iterator is called, the iterator returns its next value. when no value in the iterator can be returned, it throws an exception named StopIteration, stop iteration.

An important feature of the iterator is that it is irreversible and can only be moved forward and cannot be rolled back.

The for loop works like this. when a for loop loops an object, it calls the iter method of this object to obtain the iterator and then calls the next method of this iterator, obtain each value contained in this iterator.

2. what is the difference between the list and iterator? How can we implement a basic iterator?

The iterator is used to calculate a value and obtain a value. The list obtains all values at a time. if there are many values, it will occupy a lot of memory.

When creating an object, how can we make the object iteratable?

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?

In my personal understanding, the generator is a special iteratable object. it is different from other iteratable objects, that is, other iteratable objects need to call the iter method, return an iterator object and execute the next method through the iterator object to obtain the value in the iterator. However, the generator can be iterated directly without executing the iter method.

In python, the generator has two forms of expression:

Function Generator: a generator defined in a common function. The return value of a statement does not use return. Instead, the yield keyword is used to return a result each time, a function cannot have multiple return statements, but multiple yield statements can be returned. each yield statement in the function returns a result, the execution status of the function will be suspended. it can be understood as paused. The next time you continue to call this function, the function will continue to be executed from the position where it was suspended.

The following is an example of a function generator:

The following example demonstrates two features of yield. The first is that a function can have multiple yield values, multiple yield values, and function generator suspension.

Def func1 ():

Yield 1

Print "The first yield execution is complete ~ "

Yield 2

Print "The second yield execution is complete ~ "

Yield 3

Print "The third yield execution is complete ~ "

For I in func1 ():

Print I

>>> 1

The first yield execution is complete ~

2

The second yield execution is complete ~

3

The third yield execution is complete ~

Generator expression: A list-like derivation method is used, but the returned object is no longer a list, but an object that can generate results as needed (generator ).

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 types of writing methods play the same role, except that the writing method is different. one is a generator expression, and the other is a function generator.

Does it look like this generator expression and list derivation? the difference is that the list derivation uses [] brackets, and the generator expression uses () parentheses?

The fact is that the syntax between them is indeed only one parentheses, but the generator expression saves more memory space.

About the generator, let's talk about it. Finally, let's make a conclusion:

The definition method of the generator is the same as that of a common function. The difference is that the generator returns a value using yield, and the function returns a value using return.

In python, the generator automatically implements the iteration protocol. when no value can be returned, a StopIteration exception is returned.

The generator returns a value using the yield statement. The yield statement suspends the status of the generator function and retains enough information to continue execution from where it leaves.

The following example compares the efficiency of list derivation and generator expression execution. if you are interested, you can try it on your computer.

# List parsing

Sum ([I for I in range (100000000)]) # high memory usage, easy to kill

# Generator expression

Sum (I for I in range (100000000) # almost no memory occupied

The above is a detailed introduction to the python iterator and generator. For more information, see other related articles in the first PHP community!

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.