Difference between Python's Generators and Iterators

后端 未结 11 1219
谎友^
谎友^ 2020-11-22 05:20

What is the difference between iterators and generators? Some examples for when you would use each case would be helpful.

相关标签:
11条回答
  • 2020-11-22 05:22

    iterator is a more general concept: any object whose class has a __next__ method (next in Python 2) and an __iter__ method that does return self.

    Every generator is an iterator, but not vice versa. A generator is built by calling a function that has one or more yield expressions (yield statements, in Python 2.5 and earlier), and is an object that meets the previous paragraph's definition of an iterator.

    You may want to use a custom iterator, rather than a generator, when you need a class with somewhat complex state-maintaining behavior, or want to expose other methods besides __next__ (and __iter__ and __init__). Most often, a generator (sometimes, for sufficiently simple needs, a generator expression) is sufficient, and it's simpler to code because state maintenance (within reasonable limits) is basically "done for you" by the frame getting suspended and resumed.

    For example, a generator such as:

    def squares(start, stop):
        for i in range(start, stop):
            yield i * i
    
    generator = squares(a, b)
    

    or the equivalent generator expression (genexp)

    generator = (i*i for i in range(a, b))
    

    would take more code to build as a custom iterator:

    class Squares(object):
        def __init__(self, start, stop):
           self.start = start
           self.stop = stop
        def __iter__(self): return self
        def __next__(self): # next in Python 2
           if self.start >= self.stop:
               raise StopIteration
           current = self.start * self.start
           self.start += 1
           return current
    
    iterator = Squares(a, b)
    

    But, of course, with class Squares you could easily offer extra methods, i.e.

        def current(self):
           return self.start
    

    if you have any actual need for such extra functionality in your application.

    0 讨论(0)
  • 2020-11-22 05:25

    Everybody has a really nice and verbose answer with examples and I really appreciate it. I just wanted to give a short few lines answer for people who are still not quite clear conceptually:

    If you create your own iterator, it is a little bit involved - you have to create a class and at least implement the iter and the next methods. But what if you don't want to go through this hassle and want to quickly create an iterator. Fortunately, Python provides a short-cut way to defining an iterator. All you need to do is define a function with at least 1 call to yield and now when you call that function it will return "something" which will act like an iterator (you can call next method and use it in a for loop). This something has a name in Python called Generator

    Hope that clarifies a bit.

    0 讨论(0)
  • 2020-11-22 05:25

    Generator Function, Generator Object, Generator:

    A Generator function is just like a regular function in Python but it contains one or more yield statements. Generator functions is a great tool to create Iterator objects as easy as possible. The Iterator object returend by generator function is also called Generator object or Generator.

    In this example I have created a Generator function which returns a Generator object <generator object fib at 0x01342480>. Just like other iterators, Generator objects can be used in a for loop or with the built-in function next() which returns the next value from generator.

    def fib(max):
        a, b = 0, 1
        for i in range(max):
            yield a
            a, b = b, a + b
    print(fib(10))             #<generator object fib at 0x01342480>
    
    for i in fib(10):
        print(i)               # 0 1 1 2 3 5 8 13 21 34
    
    
    print(next(myfib))         #0
    print(next(myfib))         #1
    print(next(myfib))         #1
    print(next(myfib))         #2
    

    So a generator function is the easiest way to create an Iterator object.

    Iterator:

    Every generator object is an iterator but not vice versa. A custom iterator object can be created if its class implements __iter__ and __next__ method (also called iterator protocol).

    However, it is much easier to use generators function to create iterators because they simplify their creation, but a custom Iterator gives you more freedom and you can also implement other methods according to your requirements as shown in the below example.

    class Fib:
        def __init__(self,max):
            self.current=0
            self.next=1
            self.max=max
            self.count=0
    
        def __iter__(self):
            return self
    
        def __next__(self):
            if self.count>self.max:
                raise StopIteration
            else:
                self.current,self.next=self.next,(self.current+self.next)
                self.count+=1
                return self.next-self.current
    
        def __str__(self):
            return "Generator object"
    
    itobj=Fib(4)
    print(itobj)               #Generator object
    
    for i in Fib(4):  
        print(i)               #0 1 1 2
    
    print(next(itobj))         #0
    print(next(itobj))         #1
    print(next(itobj))         #1
    
    0 讨论(0)
  • 2020-11-22 05:27

    Iterators:

    Iterator are objects which uses next() method to get next value of sequence.

    Generators:

    A generator is a function that produces or yields a sequence of values using yield method.

    Every next() method call on generator object(for ex: f as in below example) returned by generator function(for ex: foo() function in below example), generates next value in sequence.

    When a generator function is called, it returns an generator object without even beginning execution of the function. When next() method is called for the first time, the function starts executing until it reaches yield statement which returns the yielded value. The yield keeps track of i.e. remembers last execution. And second next() call continues from previous value.

    The following example demonstrates the interplay between yield and call to next method on generator object.

    >>> def foo():
    ...     print "begin"
    ...     for i in range(3):
    ...         print "before yield", i
    ...         yield i
    ...         print "after yield", i
    ...     print "end"
    ...
    >>> f = foo()
    >>> f.next()
    begin
    before yield 0            # Control is in for loop
    0
    >>> f.next()
    after yield 0             
    before yield 1            # Continue for loop
    1
    >>> f.next()
    after yield 1
    before yield 2
    2
    >>> f.next()
    after yield 2
    end
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    StopIteration
    >>>
    
    0 讨论(0)
  • 2020-11-22 05:28

    Adding an answer because none of the existing answers specifically address the confusion in the official literature.

    Generator functions are ordinary functions defined using yield instead of return. When called, a generator function returns a generator object, which is a kind of iterator - it has a next() method. When you call next(), the next value yielded by the generator function is returned.

    Either the function or the object may be called the "generator" depending on which Python source document you read. The Python glossary says generator functions, while the Python wiki implies generator objects. The Python tutorial remarkably manages to imply both usages in the space of three sentences:

    Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the yield statement whenever they want to return data. Each time next() is called on it, the generator resumes where it left off (it remembers all the data values and which statement was last executed).

    The first two sentences identify generators with generator functions, while the third sentence identifies them with generator objects.

    Despite all this confusion, one can seek out the Python language reference for the clear and final word:

    The yield expression is only used when defining a generator function, and can only be used in the body of a function definition. Using a yield expression in a function definition is sufficient to cause that definition to create a generator function instead of a normal function.

    When a generator function is called, it returns an iterator known as a generator. That generator then controls the execution of a generator function.

    So, in formal and precise usage, "generator" unqualified means generator object, not generator function.

    The above references are for Python 2 but Python 3 language reference says the same thing. However, the Python 3 glossary states that

    generator ... Usually refers to a generator function, but may refer to a generator iterator in some contexts. In cases where the intended meaning isn’t clear, using the full terms avoids ambiguity.

    0 讨论(0)
  • 2020-11-22 05:28

    You can compare both approaches for the same data:

    def myGeneratorList(n):
        for i in range(n):
            yield i
    
    def myIterableList(n):
        ll = n*[None]
        for i in range(n):
            ll[i] = i
        return ll
    
    # Same values
    ll1 = myGeneratorList(10)
    ll2 = myIterableList(10)
    for i1, i2 in zip(ll1, ll2):
        print("{} {}".format(i1, i2))
    
    # Generator can only be read once
    ll1 = myGeneratorList(10)
    ll2 = myIterableList(10)
    
    print("{} {}".format(len(list(ll1)), len(ll2)))
    print("{} {}".format(len(list(ll1)), len(ll2)))
    
    # Generator can be read several times if converted into iterable
    ll1 = list(myGeneratorList(10))
    ll2 = myIterableList(10)
    
    print("{} {}".format(len(list(ll1)), len(ll2)))
    print("{} {}".format(len(list(ll1)), len(ll2)))
    

    Besides, if you check the memory footprint, the generator takes much less memory as it doesn't need to store all the values in memory at the same time.

    0 讨论(0)
提交回复
热议问题