Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Python iterators formally require an __iter__ method?

Tags:

Several places online, including answers in Stack Overflow (such as this, this & this), mention that Python iterators must implement both the __next__ method (which I understand) and the __iter__ method. These places rightfully conclude that all iterators are also iterables. Even PyCharm issues a type warning if a variable which is annotated to be a typing.Iterator doesn't implement that __iter__ method.

Contrary to those, the official Python tutorial section on iterators only mentions the need for a __next__ method:

The function returns an iterator object that defines the method __next__() which accesses elements in the container one at a time

So my question is: do Python iterators formally need to be iterables on their own? I personally don't understand why this should be true, and why we can't fully separate the requirements for an Iterable and an Iterator.

like image 654
Dean Gurvitz Avatar asked May 08 '20 10:05

Dean Gurvitz


People also ask

What does __ ITER __ do in Python?

The __iter__() function returns an iterator for the given object (array, set, tuple, etc. or custom objects). It creates an object that can be accessed one element at a time using __next__() function, which generally comes in handy when dealing with loops.

What is the alternative if the __ ITER __ method is not defined in a custom class to make it iterable?

1) An alternative way to make an iterable is to supply a __getitem__() method that accepts consecutive indices and raises IndexError when complete. This is how str objects iterated in Python 2.

Which method is used to define iterator?

Iterator in python is an object that is used to iterate over iterable objects like lists, tuples, dicts, and sets. The iterator object is initialized using the iter() method. It uses the next() method for iteration.

Does Python have iterators?

An iterator in Python is an object that contains a countable number of elements that can be iterated upon. In simpler words, we can say that Iterators are objects that allow you to traverse through all the elements of a collection and return one element at a time.


1 Answers

That's the tutorial. It glosses over things. If you check the data model documentation, you'll see an explicit requirement that iterators support __iter__:

The iterator objects themselves are required to support the following two methods, which together form the iterator protocol:

iterator.__iter__()

Return the iterator object itself. This is required to allow both containers and iterators to be used with the for and in statements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.

iterator.__next__()

...

Python easily could have been designed to make iterators non-iterable, the way Java did, but that would have been counterproductive. Iterating over iterators is extremely common and standard in Python, and it was always intended to be.

Having to stick some kind of iterwrapper around iterators in every for loop would be like having to stick some kind of addablewrapper around your integers every time you wanted to add two integers.

like image 182
user2357112 supports Monica Avatar answered Sep 27 '22 18:09

user2357112 supports Monica