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

后端 未结 3 1063
难免孤独
难免孤独 2020-12-17 16:07

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

next(iterator[, default])

Retrieve the next item from the i         


        
相关标签:
3条回答
  • 2020-12-17 16:23

    It calls __next__ internally, but it makes it look more 'functional' than 'object-oriented'. Mind you that's just my opinion, but I don't like next(i) rather than i.next(). But as Steve Mc said, it also helps slightly with consistency.

    0 讨论(0)
  • 2020-12-17 16:24

    It's just for consistency with functions like len(). I believe next(i) calls i.__next__() internally.

    See http://www.python.org/dev/peps/pep-3114/

    0 讨论(0)
  • 2020-12-17 16:34

    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.

    0 讨论(0)
提交回复
热议问题