Short-circuit Logic

Chris Angelico rosuav at gmail.com
Thu May 30 11:56:09 EDT 2013


On Fri, May 31, 2013 at 1:02 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 05/30/2013 05:58 AM, Chris Angelico wrote:
>> If you iterate from 1000 to 173, you get nowhere. This is the expected
>> behaviour; this is what a C-style for loop would be written as, it's
>> what range() does, it's the normal thing. Going from a particular
>> starting point to a particular ending point that's earlier than the
>> start results in no iterations. The alternative would be an infinite
>> number of iterations, which is far far worse.
>
> If the bug is the extra three zeros (maybe it should have been two), then
> silently skipping the loop is the "far, far worse" scenario.  With the
> infinite loop you at least know something went wrong, and you know it pretty
> darn quick (since you are testing, right? ;).

You're assuming you can casually hit Ctrl-C to stop an infinite loop,
meaning that it's trivial. It's not. Not everything lets you do that;
or possibly halting the process will halt far more than you intended.
What if you're editing live code in something that's had uninterrupted
uptime for over a year? Doing nothing is much safer than getting stuck
in an infinite loop. And yes, I have done exactly that, though not in
Python. Don't forget, your start/stop figures mightn't be constants,
so you might not see it in testing. I can't imagine ANY scenario where
you'd actually *want* the infinite loop behaviour, while there are
plenty where you want it to skip the loop, and would otherwise have to
guard it with an if.

ChrisA



More information about the Python-list mailing list