[Python-Dev] Re: The iterator story

Oren Tirosh oren-py-d@hishome.net
Sat, 20 Jul 2002 09:39:26 -0400


On Sat, Jul 20, 2002 at 05:45:48AM -0700, Ka-Ping Yee wrote:
> > To summarize, I agree that "for" mutating the object can be surprising.
> 
> The rub is, the only way for it to *not* be surprising is to have a
> way to *say* "loop destructively".  If you can't express your
> expectations, there's no way to meet them.

It doesn't seem very useful to say "loop destructively" - in these cases I
don't usually care whether it's destructive or not.  It is useful, though, 
to be able to say "loop INdestructively".  

That's how I do it:

def reiter(obj):
    """ Return an object's iterator, raise exception if object does not
appear to support multiple iterations """
    assert not isintance(obj, file)
    itr = iter(obj)
    assert itr is not obj
    return itr

	Oren