Iterators and generators and the data they generate

  • #1
fog37
1,549
107
TL;DR Summary
Iterators and generators and the data they generate
Hello,
I have been focusing on iterators and generators and I understood a lot but still have some subtle questions...

An iterator, which is a Python object with both the __iter__ and the __next__ methods, saves its current state. Applying the next() method to an iterator gives us one item of data at a time. On the other hand, when a regular Python list is created, all the data in the list is generated at once taking a lot of RAM. But when an iterator is created, I believe we are essentially saving the "recipe" on how to create the data but the data is generated a piece at a time and only upon our request. As we ask the iterator for data, step by step using the next() method, we are not creating and storing the data in RAM: the iterator does not save (unless we explicitly code for it) the data ahead of time and the data that is generates, correct?

Example: the data the iterator is using may already exist and be saved in the permanent memory. For example, there may be a huge text file saved on the computer. The iterator may pick a line at time from the text file without loading the entire file in RAM.
The iterator may also generate its data dynamically. For example, when we use an iterator to generate an infinite set of numbers: we don't really create those numbers in memory ahead of time or even save them after they are generated...I believe..

A generator is a special type of function with the return statement replaced by the yield statement. Is a generator just a function whose outcome/return is an iterator? Is a generator essentially a way to create a custom iterator? Python has iterator objects like range(), map(), etc. We can also convert certain iterable data structures, like lists, dictionaries, tuples, etc. into iterators using the iter() method... Are generators a way to create flexible iterators?
 
Technology news on Phys.org
  • #2
fog37 said:
the iterator does not save (unless we explicitly code for it) the data ahead of time and the data that is generates, correct?
Correct. It generates the data only as it is needed.

fog37 said:
Example: the data the iterator is using may already exist and be saved in the permanent memory. For example, there may be a huge text file saved on the computer. The iterator may pick a line at time from the text file without loading the entire file in RAM.
Yes.

fog37 said:
The iterator may also generate its data dynamically. For example, when we use an iterator to generate an infinite set of numbers: we don't really create those numbers in memory ahead of time or even save them after they are generated...I believe..
Yes. For example, look at the count function in the itertools module.

fog37 said:
A generator is a special type of function with the return statement replaced by the yield statement.
More precisely, a generator is any function that has one or more yield statements in its body. It can also have return statements in its body, although this is very rarely done. (A return inside a generator causes it to stop iteration immediately.)

fog37 said:
Is a generator just a function whose outcome/return is an iterator?
Not quite. Calling the generator function returns a generator object, which can be iterated over like any other iterable, for example in a for loop. Calling iter on an iterable (whether it's a generator or any other iterable) returns an iterator (although usually you don't need to do this explicitly, it gets done implicitly inside the interpreter when you use something like a for loop).

fog37 said:
Is a generator essentially a way to create a custom iterator?
Essentially, yes. But see above for some important details.

fog37 said:
Python has iterator objects like range(), map(), etc.
Actually, those aren't iterators, they are iterables. An iterable has an __iter__ method, but not a __next__ method. The built-in iter function implicitly calls the __iter__ method of an iterable to return an iterator over that iterable. The Python documentation goes into a fair bit of detail about all this.

fog37 said:
We can also convert certain iterable data structures, like lists, dictionaries, tuples, etc. into iterators using the iter() method... Are generators a way to create flexible iterators?
I'm not sure what you mean by "flexible".
 
  • Like
Likes fog37

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
22
Views
854
  • Programming and Computer Science
Replies
11
Views
953
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
10
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top