A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2002-July/026223.html below:

[Python-Dev] Re: Single- vs. Multi-pass iterability

[Python-Dev] Re: Single- vs. Multi-pass iterability [Python-Dev] Re: Single- vs. Multi-pass iterabilityFrançois Pinard pinard@iro.umontreal.ca
09 Jul 2002 08:14:38 -0400
[Oren Tirosh]

> class ifile(file):
>     def __iter__(self):
>         return self

>     def next(self):
>         s = self.readline()
>         if s:
>             return s
>         raise StopIteration

> class xfile:
>     def __init__(self, filename):
>         self.filename = filename

>     def __iter__(self):
>         return ifile(self.filename)

> This pair of objects has a proper container/iterator relationship.

This is all clear to me, except for one little thing.  I wonder why class
`ifile' has an `__iter__' method itself.  I know it is said to be the
"iterator protocol", and I wonder why it has to be.

My understanding is that `__iter__' returns an iterator all ready to be
enquired a number of times through `.next()' calls, and I presume that
if any re-initialisation has to take place, it is within `__iter__'.
However, as the iterator maintains its own progressive state, I do not see
the intent and purpose of the iterator having an `__iter__' method itself.
Would it make sense using the iterator `__iter__' as the preferred place
where it re-initialises itself?

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4