Iterators/Generators Flashcards

1
Q

How to add an iterator to an object

A

Implement the __iter__ magic method

Can either return self, in which case the class needs to implement __next__
Or can return an iterator class, which has these methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why can you iterate over an array without __iter__

A

Because it implements __getitem__ and the first index is 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly