newbie - infinite loop

Paul Brian pbrian at demon.net
Mon Mar 19 11:29:19 EST 2001


All,

Thank you for such quick responses.

I shall take it as final that a for loop is expandable if the condition is
mutable.  I just thought I might have missed something else.

And thank you all for pointing out the deliberate :-) mistake over matching
and not matching

"Remco Gerlich" <scarblac at pino.selwerd.nl> wrote in message
news:slrn9bc8et.95q.scarblac at pino.selwerd.nl...
> Paul Brian <pbrian at demon.net> wrote in comp.lang.python:
> > I have solved the opriginal problem but am left with a serious question.
> >
> > Before I discovered the keywords in and not in, I tried to count unique
> > instances of names in a file.
> >
> Your code belows does the opposite: if it *is* already in the list, append
> it once more. And since you change the list you're currently looping over
> (which is a mortal sin, etc) you get an infinite loop.
> >
> It's a list. It's mutable. It's not a "for condition", for just tries to
get
> new elements from the list until that gives an error, then the loop is
> finished. If you append to the list during the loop, the new elements are
> also visited. If you add or delete things in the middle of the list,
> anything might happen.
>
> Do not taunt happy fun for loops. Do not change lists you are looping
over.






More information about the Python-list mailing list