Alex Rivera | Logout

Why did Python 2.6 add a global next() function?

Asked 2009-03-17T21:42:39.053
15

I noticed that Python2.6 added a next() to it's list of global functions.

next(iterator[, default])

Retrieve the next item from the iterator by calling its next() method.

If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.

What was the motivation for adding this? What can you do with next(iterator) that you can't do with iterator.next() and an except clause to handle the StopIteration?

Edit
Report

1 Answer

10

Note that in Python 3.0+ the next method has been renamed to __next__. This is because of consistency. next is a special method and special methods are named by convention (PEP 8) with double leading and trailing underscore. Special methods are not meant to be called directly, that's why the next built in function was introduced.

answered 2009-03-18T00:31:36.823

Your Answer