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

Aahz aahz@pythoncraft.com
Fri, 19 Jul 2002 12:58:37 -0400


On Fri, Jul 19, 2002, Fredrik Lundh wrote:
> aahz wrote:
>> 
>> While technically true, that seems to be sidestepping the point from my
>> POV.
> 
> really?  are you arguing that when Ping says that for-in shouldn't
> destroy the target, he's really saying that python shouldn't allow
> methods to have side effects if they can be called from an
> expression used in a for-in statement?  why would he say that?

I'm saying that I think Ping is overstating the case in terms of the way
people look at things.  Whatever the technicalities of an implicit
method versus an explicit method, people have long used for loops in
destructive ways.

>> I think that few people see for loops as inherently non-destructive
>> due to the use case I presented above.
> 
> I think most people can tell the difference between an object and
> a method with side-effects.  I doubt they would be able to get much
> done in Python if they couldn't.

To be sure.  But I don't think there's much difference in the way for
loops are actually used.  Continuing my point above, I see the current
usage of for loops as calling an implicit method with side-effects as
opposed to an explicit method with side-effects.  Lo and behold!  That's
actually the case.

>> Beyond that, the for loop is itself inherently mutating in Python
>> older than 2.2
> 
> in what sense?  it calls the object's __getitem__ method with an
> integer index value, until it gets an IndexError.  in what way is that
> "inherently mutating"?

And how does that integer index change?  The for loop in Python <2.2 has
an internal state object.  Iterators are the external manifestation of
that state object, generalized to objects other than sequences.  I'm
surprised that anyone is surprised that the state object gets
mutated/destroyed.  I'm also surprised that people are surprised about
what happens when that state object is coupled to an inherently mutating
object such as file objects.
-- 
Aahz (aahz@pythoncraft.com)           <*>         http://www.pythoncraft.com/

Project Vote Smart: http://www.vote-smart.org/