for loop without variable

Mike Meyer mwm-keyword-python.b4bdba at mired.org
Thu Jan 10 00:43:35 EST 2008


On Wed, 9 Jan 2008 18:49:36 -0800 (PST) erik gartz <eegunnar at yahoo.com> wrote:
> The loop performs some actions with web services. The particular
> iteration I'm on isn't important to me. It is only important that I
> attempt the web services that number of times. If I succeed I
> obviously break out of the loop and the containing function (the
> function which has the loop in it) returns True. If all attempts fail
> the containing loop returns False.

It sounds to me like your counter variable actually has meaning, but
you've hidden that meaning by giving it the meaningless name "i". If
you give it a meaningful name, then there's an obvious way to do it
(which you listed yourself):

    while retries_left:
       if this_succeeds():
          return True
       retries_left -= 1

    return False

    <mike
-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.



More information about the Python-list mailing list