Proposed new syntax

Chris Angelico rosuav at gmail.com
Fri Aug 25 00:26:41 EDT 2017


On Fri, Aug 25, 2017 at 2:10 PM, Paul Rubin <no.email at nospam.invalid> wrote:
> Steve D'Aprano <steve+python at pearwood.info> writes:
>> Did __next__ cache the most recently generated value?
>
> No but if they're going to change stuff, they might as well actually
> improve it instead of just renaming it to break code gratutiously.

Any code that called iter.next() instead of next(iter) was already
broken. Converting it to the proper notation makes it work on all
Python versions. Any code that needs to *implement* an iterator can do
so fairly simply:

class Foo(object): # you already need (object) for Py2 compat
    def __iter__(self): return self
    def __next__(self):
        # whatever
        return 42
    next = __next__ # this is all you need to add

Considering that it then brings the protocol nicely in line with every
other magic-method protocol in Python, this is a Good Thing. It's one
more line of code in any class that is an iterator - not all
iterables, only custom iterators. It's no additional code in consumers
of iterators.

ChrisA



More information about the Python-list mailing list