On 31/08/14 14:25, Crush wrote:
Alan said,

"... you are reassigning p in the middle of a loop that
depends on p. That's usually not a good idea..."

If not what I changed my code to, then I reckon I do not
> understand what he is suggesting I do.

Your original code looked like this:

p = subprocess()
for line in p.stderr:
    if some condition:
       p = subprocess()   # now reassigned p to a new subprocess
       for line in p.stderr:
           do stuff

So the question is:
What happens to the outer loop when you finish 'do stuff'?

It may be that it is doing what you want, or it may be that
you want to break the outer loop when your some condition
succeeds. I'm not sure?

If you do want to exit the outer loop a more explicit
way would be to set a flag and break:

p = subprocess()
for line in p.stderr:
    if some condition:
       some_flag = True
       break
    elif other condition:
       other_flag = True
       break

if some_flag:
    p = subprocess()
    for line in p.stderr:
        do stuff
elif other_flag:
    do other stuff

But I haven't read through the code in enough detail to be
sure what you are doing, it was just a general observation
that resetting the the thing that your loop depends on
is usually a bad idea. A bit like cutting off the branch
of the tree that you are standing on...

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to