I think this analogy is more like a metaphor, used playfully by the author to get you to think conceptually about what a generator is.
An Iterator is an object that knows how to access items from a
collection one at a time, while keeping track of its current position
next() method which returns the next item in the sequence.
This method can optionally raise a StopIteration exception when the
sequence is exhausted.
Generators provide a powerful alternative: they allow you to define an iterative algorithm by writing a single function which can maintain its own state, and then returning individual values based on that persistent state using the
In short, an iterator is a function that returns one of a succession of data points each time it is called, and a generator is just some syntactic sugar that allows you to create such a function with the syntax and ease of use of an ordinary method.
The data points returned individually from an iterator method can come from an array or collection, or they can be generated by code. A pseudo-random generator can be thought of as an iterator, since giving it the same initialization seed will return the same sequence of values.
So what is a derivative? Put succinctly, it is a function that, at any given point on a curve returns the slope of a line at a tangent to the curve at that point. It looks something like this:
Well, that does look like an iterator, doesn't it? The graphic is basically illustrating a function that accepts multiple, successive positions on a curve, and returns the slope of the line at each point on that curve, one value at a time.
The only problem with this analogy is that an iterator doesn't have to return derivative values; it can return anything. The derivative values are just a convenient way of forcing you to think about the nature of iterators.
A better analogy might be a Pez dispenser.