Short-circuit Logic

Chris Angelico rosuav at gmail.com
Thu May 30 08:58:27 EDT 2013


On Thu, May 30, 2013 at 10:40 PM, Roy Smith <roy at panix.com> wrote:
> if somebody were to accidentally drop three zeros into the source code:
>
>> x = 1000
>> while x < 173:
>>     print(x)
>>     x += 1
>
> should the loop just quietly not execute (which is what it will do
> here)?  Will that make your program correct again, or will it simply
> turn this into a difficult to find bug?  If you're really worried about
> that, why not:

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.

ChrisA



More information about the Python-list mailing list