[issue40579] Second argument to iterator.next not described

New submission from Andrew Black <apblack@pdx.edu>: The documentation for the built-in function next (which calls the __next__ method on an iterator) discusses its behavior when the iterator is exhausted. It talks about the StopIteration exception. However, it does not say anything about calling next with two arguments. See the library documentation at https://docs.python.org/3/library/stdtypes.html#iterator-types My impression was that the presence of the second argument would suppress the StopIteration exception; instead next would return the value of the second argument. That is the way that list iterators behave, for example. This behavior should be documented. ---------- assignee: docs@python components: Documentation messages: 368525 nosy: Andrew Black, docs@python priority: normal severity: normal status: open title: Second argument to iterator.next not described type: behavior versions: Python 3.8 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40579> _______________________________________

Rémi Lapeyre <remi.lapeyre@henki.fr> added the comment: It's documented here: https://docs.python.org/3/library/functions.html#next ---------- nosy: +remi.lapeyre _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40579> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: The next() function has a different signature than the __next__() method. The former takes one or two arguments. That latter only takes one. >>> it = iter('abcde') >>> next(it) 'a' >>> next(it, 'default') 'b' >>> it.__next__() 'c' >>> it.__next__('default') Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> it.__next__('default') TypeError: expected 0 arguments, got 1 ---------- nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40579> _______________________________________

Change by Rahul Kumaresan <kayrahul@gmail.com>: ---------- nosy: +rahul-kumi _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40579> _______________________________________

Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- resolution: -> not a bug stage: -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue40579> _______________________________________
participants (4)
-
Andrew Black
-
Rahul Kumaresan
-
Raymond Hettinger
-
Rémi Lapeyre