26

Python 2.6 introduced a next function.

Why was this necessary? One could always type obj.next() instead of next(obj).

Is the latter more pythonic?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Dave Halter
  • 15,556
  • 13
  • 76
  • 103
  • 6
    I've always wondered why the Monty Python-inspired Python language felt the need to coin the term "pythonic" instead of the more natural "Pythonesque"..... – David M May 02 '12 at 12:58
  • Also asked before: http://stackoverflow.com/questions/656155/why-did-python-2-6-add-a-global-next-function – Russia Must Remove Putin Mar 05 '14 at 22:24

3 Answers3

39

PEP 3114 describes this change. An excerpt about the motivation:

This PEP proposes that the next method be renamed to __next__, consistent with all the other protocols in Python in which a method is implicitly called as part of a language-level protocol, and that a built-in function named next be introduced to invoke __next__ method, consistent with the manner in which other protocols are explicitly invoked.

Be sure to read this PEP for more interesting details.

As for why you want to use the next built-in: one good reason is that the next method disappears in Python 3, so for portability it's better to start using the next built-in as soon as possible.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 1
    I liked this answer most. Especially because of the note, that Py3000 won't have next methods on objects anymore. – Dave Halter May 03 '12 at 20:51
28

next(iterator[, default])

Retrieve the next item from the iterator by calling its next()(__next__() in python 3) method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.

You get the default option.

Eric
  • 95,302
  • 53
  • 242
  • 374
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • 1
    +1, but couldn't one then simply have added an optional `default` parameter to the method: `obj.next(default)`? – Tim Pietzcker May 02 '12 at 13:00
  • 11
    @Tim: Then every iterator implementation would have to duplicate the logic. – Niklas B. May 02 '12 at 13:02
  • 5
    Also, in Python 3 the `next()` method does not exist anymore. It is replaced by `__next__()`. The `next()` function allows a unified behaviour. – rubik May 02 '12 at 13:07
12

Apart from the obvious additional functionality, it also looks better when used together with generator expressions. Compare

(x for x in lst if x > 2).next()

to

next(x for x in lst if x > 2)

The latter is a lot more consistent with the rest of Python's style, IMHO.

Niklas B.
  • 92,950
  • 18
  • 194
  • 224
  • Doesn't the generator expression start over from the beginning each time the statement is executed? Seems like this would only ever return the first element. – Mark Ransom May 03 '12 at 16:21
  • 4
    @Mark: Yes, that's the purpose of this idiom: Return the first element from an iterable that meets a certain criterion (or raise `StopIteration` otherwise). – Niklas B. May 03 '12 at 16:24
  • It's like `Enumerable#find` in Ruby, for example. – Niklas B. May 03 '12 at 17:03