7

I am newbie to Python. I was able to understand Iterables and Iterators. However I have seen that there is lot of stuff that compares Generators vs Iterators.

As per understanding, Iterable is an object which actually has elements stored inside it (E.g. a list). They follow an iteration protocol where they implement __iter__() method which returns an Iterator object which helps in iterating the Iterable.

As per my understanding Generators helps in generating the data on the fly instead of creating a big data structure in memory and returning it. We can achieve simialr goal by the use of Iterators as well.

Now my doubt, If we already had Iterators what was the need of Generators, since both helps acheiving a similar goal of generating data on the fly. Is that just to simplify the syntax or is there any other reason why Generators exist ?

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
Sumit Trehan
  • 3,985
  • 3
  • 27
  • 42
  • Generators are iterators, they are just a simplified version of an iterator that uses the `yield` syntax, but they have less functionalities. This post covers everything you need to know, possibly a dupe: https://stackoverflow.com/q/2776829/6622817 – Taku Dec 11 '17 at 06:16
  • 2
    A generator is technically an iterator, basically, it's a way to define iterator protocol in a compact way. A classic iterator will be defined using a class with `__iter__` and `__next__` methods, with a generator you can do this with just a function with yield statements or generator expressions. – Ashwini Chaudhary Dec 11 '17 at 06:17
  • So generators exists just to simplify the syntax or is there any othere reason ? – Sumit Trehan Dec 11 '17 at 06:20
  • That's it! Just simplifies things so it's easier to read. – jhpratt Dec 11 '17 at 06:22
  • An _iterable_ is anything you can iterate over. – PM 2Ring Dec 11 '17 at 06:23
  • 3
    I think [Aaron Hall♦'s answer](https://stackoverflow.com/a/28353158/4014959) at the linked question covers everything nicely. – PM 2Ring Dec 11 '17 at 06:34
  • 1
    https://noobknowledge.blogspot.in/ – Great Coder Dec 13 '17 at 07:04

1 Answers1

20

Here's how these terms are defined in the glossary in the official Python documentation.

iterable

An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like dict, file objects, and objects of any classes you define with an __iter__() method or with a __getitem__() method that implements Sequence semantics.

Iterables can be used in a for loop and in many other places where a sequence is needed (zip(), map(), …). When an iterable object is passed as an argument to the built-in function iter(), it returns an iterator for the object. This iterator is good for one pass over the set of values. When using iterables, it is usually not necessary to call iter() or deal with iterator objects yourself. The for statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop. See also iterator, sequence, and generator.

iterator

An object representing a stream of data. Repeated calls to the iterator’s __next__() method (or passing it to the built-in function next()) return successive items in the stream. When no more data are available a StopIteration exception is raised instead. At this point, the iterator object is exhausted and any further calls to its __next__() method just raise StopIteration again. Iterators are required to have an __iter__() method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. One notable exception is code which attempts multiple iteration passes. A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.

More information can be found in Iterator Types.

generator

A function which returns a generator iterator. It looks like a normal function except that it contains yield expressions for producing a series of values usable in a for-loop or that can be retrieved one at a time with the next() function.

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.

generator iterator

An object created by a generator function.

Each yield temporarily suspends processing, remembering the location execution state (including local variables and pending try-statements). When the generator iterator resumes, it picks-up where it left-off (in contrast to functions which start fresh on every invocation).

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182