[Python-3000] PEP: rename it.next() to it.next(), add a next() built-in (original) (raw)

Terry Reedy tjreedy at udel.edu
Mon Mar 5 23:54:58 CET 2007


<python at rcn.com> wrote in message news:20070305123101.BAH59075 at ms09.lnh.mail.rcn.net... | Can I suggest that next() and next() be dropped entirely and that iterators just be made callable.

Can you do that without making the call a regular, slow call each time? Or at least without adding a lookup of 'call' each time?

For generators, I could imagine renaming .next to .call, but in general, how would you mark something as an iterator (versus non-iterator iterable or anything else) without .next (or the proposed .next), since that is the very thing that now marks it (along with .iter) as an iterator. In other words, what would a user-defined iterator class look like?

If you say .call along with .iter, then you are stipulating that non-iterator iterables cannot be callable themselves. I have no idea it this would break code.

| 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.

No me. So 'some people' rather than 'people'. How many? Learning about method binding is fairly important. Would changing .next to .call really solve whatever problem you think there is?

| >>> ### What we do now | >>> import itertools | >>> count = itertools.count(1).next | >>> count() | 1 | >>> count() | 2 ||| >>> ### What would be nice

But, I believe slower, due to the repeated .call method name lookup

| >>> import itertools | >>> cnt = itertools.count(1) | >>> cnt() | 1 | >>> cnt() | 2

so I expect the sophisticated user, if expecting to call cnt perhaps millions of times, would write

cnt = itertools.count(1).call # or whatever

Bound .next methods are perhaps unique in potentially massive reuse.

Terry Jan Reedy



More information about the Python-3000 mailing list