3

Possible Duplicate:
Difference between Python Generators vs Iterators

Generators seem like big deal in Python, new features are added to them now and then and so on.

As far as I can see, instead generators you could always use an object with iterator interface. Is (usually) better conciseness the only benefit of generators or am I missing something?

Community
  • 1
  • 1
Aivar
  • 6,814
  • 5
  • 46
  • 78

2 Answers2

15

Yes, iterators are a more general construct, and anything you could do with a generator could be done with an iterator.

However, generators are really nice tool to express certain ideas in a very clean and concise fashion, for which iterators would become cumbersome.

For example, here's a simple function:

def count_to(m):
    n = 0
    while n <= m:
        yield n
        n += 1

Nice and easy. Here's the same thing as an iterator:

class CountTo:
    def __init__(self, m):
        self.m = m
        self.n = 0
    def __iter__(self):
        return self
    def next(self):
        if self.n <= self.m:
            cur, self.n = self.n, self.n + 1
            return cur
        else:
            raise StopIteration()

One is 5 lines, the other is 12. The generator expresses the iteration process very succintly, while the iterator obfuscates it with explictly-maintained state and boilerplate code.

A lot of Python's philosophy is based around readability and simplicity. In keeping with this, I feel that generators offer a nicer interface for a broad class of tasks that would otherwise require iterators. Yes, iterators are more powerful, but the syntactic advantages of generators certainly can't be overlooked.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Thanks! I agree that first version looks cleaner, both visually and semantcally, but the trade-off is that you need to introduce/learn a new language construct. I was just worrying that maybe I was overlooking something else. – Aivar Oct 07 '12 at 06:47
4

The difference between iterators and generators is that generator does lazy evaluation, it generates values on demand, where iterator evaluates on every iteration and stores them in memory. Generators are better for huge loops, as they only "hold" one value at the time.

razi
  • 97
  • 1
  • 3
  • 1
    Why does an interator have to create values at start? It can generate each new value in "next" method – Aivar Oct 07 '12 at 06:40
  • 1
    @razi: Sorry, this isn't how iterators work in Python. Iterators can be just as lazy as generators. – nneonneo Oct 07 '12 at 06:42
  • Could you give me an example of a lazy iterator please? I honestly thought / think that only generators work like that, generating values on the fly, while others store it in memory. – razi Oct 07 '12 at 13:16
  • 1
    See my post. You can do `for i in CountTo(1000000000)` and it will happily give you values one-at-a-time. – nneonneo Oct 07 '12 at 13:32
  • Ah yes, well i figured it could be done in a way, since generators exist as well, but i somehow doubt anyone would make such a Class while he could just pick a generator, which is easier, shorter, and usually better optimised. Will know for next time, thank you :) – razi Oct 07 '12 at 14:12