Search the courses, subjects, tracks, games and features, or jump straight to a page.
5 cards from this section. Quick, good the night before a test.
What are the two key methods a custom iterator class must implement?
Think about how Python knows to get the next item and where the iteration starts.
What happens when __next__() raises a StopIteration exception?
This stops the loop or any iteration from continuing.
What should the __iter__() method return in a custom iterator?
It connects the iterable to the iterator.
True or False: You can use a custom iterator multiple times without reinitializing it.
Iterators maintain their position and do not reset automatically.
Create a custom iterator that yields the squares of numbers from 1 up to a given limit.
Use current to keep track and stop at the limit.
Design a custom iterator that generates the factorial of numbers starting from 1 indefinitely. (Hint: infinite iterator)
This iterator doesn’t stop, so be careful when using it.
6 more questions in this section. Enroll free to practise them all.
Enroll free