[Python-3000] PEP: rename it.next() to it.__next__(), add a next() built-in
python at rcn.com
python at rcn.com
Mon Mar 5 18:31:01 CET 2007
Can I suggest that next() and __next__() be dropped entirely and that iterators just be made callable. The current approach seems to make people think there is something wrong with using an iterator directly (inside of in a for-loop or function that consumes an iterator.
>>> ### What we do now
>>> import itertools
>>> count = itertools.count(1).next
>>> count()
1
>>> count()
2
>>> ### What would be nice
>>> import itertools
>>> cnt = itertools.count(1)
>>> cnt()
1
>>> cnt()
2
>>> def f(x):
... while x > 1:
... yield x
... if x % 2 == 0:
... x //= 2
... else:
... x = x * 3 + 1
...
>>> g = f(12)
>>> g()
12
>>> g()
6
>>> g()
3
>>> g()
10
Raymond Hettinger
More information about the Python-3000
mailing list