iterator clone

Peter Otten __peter__ at
Sun Jul 13 12:30:52 CEST 2008

Yosifov Pavel wrote:

> On 13 июл, 14:12, Peter Otten <__pete... at> wrote:
>> Yosifov Pavel wrote:
>> > Whats is the way to clone "independent" iterator? I can't use tee(),
>> > because I don't know how many "independent" iterators I need. copy and
>> > deepcopy doesn't work...
>> There is no general way. For "short" sequences you can store the items in
>> a list which is also the worst-case behaviour of tee().
>> What are you trying to do?
>> Peter
> I try to generate iterators (iterator of iterators). Peter, you are
> right! Thank you. For example, it's possible to use something like
> this:
> def cloneiter( it ):
>     """return (clonable,clone)"""
>     return tee(it)


That is too abstract, sorry. What concrete problem are you trying to solve
with your cloned iterators? There might be a way to rearrange your setup in
a way that doesn't need them.

> But I think (I'm sure!) it's deficiency of Python iterators! They are
> not very good...

Well, I think Python's iterators, especially the generators, are beautiful.
More importantly, I think there is no general way to make iterators
copyable, regardless of the programming language. The problem is that most
of the useful ones depend on external state.


More information about the Python-list mailing list