__next__ and StopIteration

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Feb 10 00:54:14 EST 2015


Chris Kaynor wrote:

> On Mon, Feb 9, 2015 at 4:42 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> so that's an excellent sign that doing so is best practice, but it should
>> not be seen as *required*. After all, perhaps you have good reason for
>> wanting your iterable class to only be iterated over once.
> 
> In fact, there is one in the stdlib, the "file" object, which has a
> __iter__ which returns self. The code below shows this,
[...]
> The "file" object is also an example of this. It is technically a
> broken iterator according to the docs:


Awesome example!

-- 
Steve




More information about the Python-list mailing list