how to avoid checking the same condition repeatedly ?

Chris Angelico rosuav at gmail.com
Mon Oct 28 07:18:45 EDT 2013


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 :)

ChrisA



More information about the Python-list mailing list