yuanlu0210
yuanlu0210

Reputation: 193

The book [Learning Python] claimed that range is a generator- - Is this a false statement?

For the sake of discussion, I will ask the following question based on Python 3.6.1

I read from the book [Learning Python] (5th edition, Chapter 20, page 614) that:

As we’ve seen, fundamental built-in tools such as range, map, dictionary keys, and even files are now generators, so you must be familiar with the concept even if you don’t write new generators of your own.

Is this a false statement?

I thought that an object being an iterator is a necessary but not sufficient condition for it to be a generator. That is, a generator’s iterator is always the generator itself.

However, range and dictionary keys are iterables but they are NOT their own iterators. If they are not iterators, how can they be generators?

Upvotes: 1

Views: 99

Answers (1)

Ned Batchelder
Ned Batchelder

Reputation: 375574

Sometimes people use "generator" to mean any lazy iterable, even though that is not the precise definition. In Python 3, range is a lazy iterable.

Upvotes: 4

Related Questions