iterator clone

Yosifov Pavel bulg at ngs.ru
Wed Jul 16 04:39:57 EDT 2008


On 16 июл, 11:32, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Tue, 15 Jul 2008 19:54:30 -0700, Yosifov Pavel wrote:
> > Kay, can you show example of such generator? ReIter, for example, work
> > with usual generators.
>
> > But for "big" iterator, I think is no any good solutions. IMHO we can
> > discern 2 types of iterators: re-startable (based on internal Python
> > objects) and not re-startable (with an external state, side-
> > effects)...
>
> Has nothing to do with internal vs. external.
> Examples: ``itertools.count(1)``, ``itertools.cycle(iterable)``, or
>
> def fib():
>     a, b = 0, 1
>     while True:
>         yield a
>         a, b = b, a + b
>
> Ciao,
>         Marc 'BlackJack' Rintsch

Yes. So, I'm disconcerted: what Python "means" about iterators. Why
iterator's are not clonable in default?.. ``itertools.count(it) ``
"suppose" ``it`` will be restarted after count? So ``count(it)``
"suppose" ``it`` is restarable and therefore clonable (why not?!).
Generator is only function, so can be restarted/cloned. Generator
keeps "position" and can't be iterated again. But this position can be
reseted (main rule: if generator function has not side-effects/
external state, see below)!

Iterator, I think, is more general method (for naturally serial
access). For example, you can read values from some device (RS323 or
other) and represent it in program like iterator. In this case,
``__iter__()`` make some preparation, ``next()`` read next value from
RS232. In this case, iterator can't be restarted and really cloned. It
has external state (state of device) and can't to return it at start.
But when series of values are generated (are born) in the program: no
problem to have the feature of clone/restart iterator.

And is very interesting to research Icon iterators. Is possible to
create something in Python such theirs, for non-deterministic solving
purpose... :-)

--pavel



More information about the Python-list mailing list