An Iterator Idiom -- another proposal
Hisao Suzuki
suzuki611 at okisoft.co.jp
Fri May 7 02:55:17 EDT 1999
In article <xj0d80gzgtf.fsf at cyclone.harvard.edu>,
Michael Haggerty <mhagger at blizzard.harvard.edu> wrote:
[snip]
> How about giving us the possibility of emulating a sequential-access
> data structure with a new magic method called, say, __getnext__, which
> would be used as follows:
>
> class iterator:
> def __init__(self, f, *args):
> self.f=f
> self.args=args
> def __getnext__(self):
> val = apply(self.f, self.args)
> if not val:
> raise IndexError, 'no more items'
> return val
>
> Then `for' loops would first try to use __getnext__ for the iteration,
> and only if that were not available it would fall back to
> __getitem__. This paradigm would be useful, I think, for many other
> situations as well.
[snip]
I second it!
--
Hisao Suzuki suzuki611 at okisoft.co.jp, suzuki at acm.org
More information about the Python-list
mailing list