The difference between Python iteratable object, iterator and generator

This article briefly discusses the relationship between iteratable objects, iterators, and generators.

Brief relationship among the three

Iteratable objects and iterators

At first, I thought that the two were the same, but later I found that they were not; here's the conclusion:

1) Iteratable objects contain iterators.
2) If an object has an iterator method, it is an iteratable object; if an object has a next method, it is an iterator.
3) To define an iteratable object, you must implement the iterator method; to define an iterator, you must implement the iterator method and the next method.

You may ask, is there a little contradiction between conclusion 3 and conclusion 2? Since an object has a next method that is an iterator, why does the iterator have to implement two methods at the same time?

Because conclusion 1, iterators are also iteratable objects, so iterators must also implement the iterator method.

Introduce the two methods mentioned above:


This method returns an instance of the iterator class of the current object. Because both iteratable objects and iterators implement this method, there are two ways to write it.

Write method 1: used to write an iteratable object class, and returns an instance of the iterator class of the iteratable object.

Write method 2: it is used to write the iterator class. It directly returns self (i.e. itself), indicating that it is its own iterator.

Maybe it's a little dizzy. It doesn't matter. Here's an example of two writing methods. Let's take a look at the specific examples.


Return to each step of the iteration. When implementing the method, note that the StopIteration exception should be thrown when the method finally goes beyond the boundary.

Here is an example of an iteratable object and iterator:

  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. class MyList(object): # Define an iterative object class
  4. def __init__(self, num):
  5. = num # Upper boundary
  6. def __iter__(self):
  7. return MyListIterator( # Returns an instance of the iterator class of the iteratable object
  8. class MyListIterator(object): # Defines the iterator class, which is the iterator class of the MyList iteratable object
  9. def __init__(self, data):
  10. = data # Upper boundary
  11. = 0 # Current iteration value, initially 0
  12. def __iter__(self):
  13. return self # Returns an instance of the object's iterator class; since it is an iterator, returns self
  14. def next(self): # Methods that iterator classes must implement
  15. while <
  16. += 1
  17. return - 1 # Returns the current iteration value
  18. raise StopIteration # Beyond the upper boundary, throw an exception
  19. my_list = MyList(5) # Get an iterative object
  20. print type(my_list) # Returns the type of the object
  21. my_list_iter = iter(my_list) # Get the iterator instance of the object. The iter function will be explained in detail below
  22. print type(my_list_iter)
  23. for i in my_list: # iteration
  24. print i

Operation result:

Problem: in the example above iter Function, what is this? and__iter__Does the method matter?
In fact, the function is closely related to the iteration, through the Python Print on command line“ help(iter)"We know that it has the following two usages.

Usage 1: iter(callable, sentinel)
Constant calls callable,Until its return value is equal to sentinel. Among them callable It can be a function, method or implementation__call__Method.

Usage two: iter(collection)
1)For return collection Iterator instance of object, here collection I think it represents an iterative object, that is, the object must implement__iter__Method;In fact iter Function and__iter__The methods are very closely linked, iter()Is directly calling the object__iter__(),And put__iter__()The return result of is used as its own return value, so this usage is often called "create iterator".
2)iter Function can display calls, or when executed“ for i in obj:",Python The interpreter calls automatically on the first iteration iter(obj),Later iterations call the iterator's next Method, for Statement will automatically handle the last thrown StopIteration Abnormal.

Through the above example, I believe that we have a more specific understanding of iteratable objects and iterators, so what is the relationship between generators and them? Let's talk about it briefly


Generator is a special kind of iterator, which implements the "iterator Protocol" automatically__iter__and next Method),There is no need to implement the two methods manually.

The generator can change the current iteration value in the process of iteration, but the modification of the current iteration value of ordinary iterators often causes exceptions,Affect the execution of procedures.

Look at an example of a generator:

  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. def myList(num): # Define generator
  4. now = 0 # Current iteration value, initially 0
  5. while now < num:
  6. val = (yield now) # Returns the current iteration value and accepts the possible send value; yield is explained below
  7. now = now + 1 if val is None else val # Val is None, and the iteration value increases by 1. Otherwise, reset the current iteration value to val
  8. my_list = myList(5) # Get a generator object
  9. print # Returns the current iteration value
  10. print
  11. my_list.send(3) # Reset the current iteration value
  12. print
  13. print dir(my_list) # Return the method name owned by the object, and you can see that "iter" and "next" are in it

Operation result:

Have yield The functions of keywords are all generators,yield It can be understood as return,Returns the subsequent value to the caller. The difference is return When returned, the function is released, and the generator is not. In the direct call next Method or use for On the next iteration of the statement, the generator yield Start the next sentence until the next one is encountered yield.

reference material:

Python Core programming second edition 11.10 Section 13.13.3 section

Fully understand Python Iterated object, iterator, generator

In-depth explanation Python Iterators and generators in

How to better understand Python Iterators and generators

If there is anything inappropriate in this article, please be tolerant and point out. Thank you

                        <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">Give the thumbs-up</span>
                        <span class="count">12</span>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">Collection</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-fenxiang"></use>
                        <!--Reward begins-->
                                                <!--End of reward-->
                                                <li class="tool-item tool-more">
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="" p-id="5717" xmlns:xlink="" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">Article report</a></li>
            <div class="person-messagebox">
                <div class="left-message"><a href="">
                    <img src="" class="avatar_pic" username="jinixin">
                                            <img src="" class="user-years">
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">jinixin</a></span>
                    <div class="text"><span>Published 56 original articles</span> · <span>Praise 91</span> · <span>150000 visitors+</span></div>
                                <div class="right-message">
                                            <a href="https://Im. CSDN. Net / im / main. HTML? Username = jinixin "target =" [blank "class =" BTN BTN SM BTN red hollow BT button personal letter "> private message
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">follow</a>
    Published 4 original articles, praised 0, visited 81
    Private letter follow

    Tags: Python Javascript Programming

    Posted on Sat, 11 Jan 2020 21:47:06 -0800 by peeps