how to avoid checking the same condition repeatedly ?

Steven D'Aprano steve at pearwood.info
Tue Oct 29 00:51:32 EDT 2013


On Mon, 28 Oct 2013 22:18:45 +1100, Chris Angelico wrote:

> On Mon, Oct 28, 2013 at 10:14 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> if needs_preprocessing:
>>     for elem in iterable:
>>         # If you change this block, you must remember to change the #
>>         block below too!
>>         preprocess(item)
>>         do_this(item)
>>         do_that(item)
>>         do_something_else(item, arg1, arg2, expression)
>>         do_another_thing(item + arg3, key=whatever) print(item)
>> else:
>>     for elem in iterable:
>>         # If you change this block, you must remember to change the #
>>         block above too!
>>         preprocess(item)
>>         do_this(item)
>>         do_that(item)
>>         do_something_else(item, arg1, arg2, expression)
>>         do_another_thing(item, key=whatever)
>>         print(item)
>>
>>
>> The careful reader will notice that there's already a bug in this.
> 
> For a start, you're preprocessing in the second block... I don't think
> that was your intentional bug :)

Heh, no, that wasn't the intentional bug. But it does go to show the 
risks of copy-and-paste programming.



-- 
Steven



More information about the Python-list mailing list