Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #57989

Re: how to avoid checking the same condition repeatedly ?

From alex23 <wuwei23@gmail.com>
Newsgroups comp.lang.python
Subject Re: how to avoid checking the same condition repeatedly ?
Date 2013-10-30 11:38 +1000
Organization A noiseless patient Spider
Message-ID <l4pnud$evn$1@dont-email.me> (permalink)
References <mailman.1693.1382953842.18130.python-list@python.org>

Show all headers | View raw


On 28/10/2013 7:50 PM, Wolfgang Maier wrote:
> imagine you have a flag set somewhere earlier in your code, e.g.,
>
> needs_processing = True
>
> then in a for loop you're processing the elements of an iterable, but the
> kind of processing depends on the flag, e.g.,:
>
> for elem in iterable:
>      if needs_processing:
>          pre_process(elem)  # reformat elem in place
>      print(elem)
>
> this checks the condition every time through the for loop, even though there
> is no chance for needs_processing to change inside the loop, which does not
> look very efficient.

There are two approaches I would consider using here:

1. Make pre_process a decorator, and outside of the loop do:

     def pre_process_decorator(fn):
         def pre_process(x):
             # functionality goes here
             return fn(x)
         return pre_process

     if needs_processing:
         print = pre_process_decorator(print)

     for elem in iterable:
         print(elem)

2. Replace the iterable with a generator if the condition is met:

     if needs_processing:
         iterable = (pre_process(x) for x in iterable)

     for elem in iterable:
         print(elem)

Personally, I find the 2nd approach clearer.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to avoid checking the same condition repeatedly ? Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-10-28 09:50 +0000
  Re: how to avoid checking the same condition repeatedly ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-28 11:14 +0000
    Re: how to avoid checking the same condition repeatedly ? Chris Angelico <rosuav@gmail.com> - 2013-10-28 22:18 +1100
      Re: how to avoid checking the same condition repeatedly ? Steven D'Aprano <steve@pearwood.info> - 2013-10-29 04:51 +0000
  Re: how to avoid checking the same condition repeatedly ? Piet van Oostrum <piet@vanoostrum.org> - 2013-10-28 14:31 -0400
  Re: how to avoid checking the same condition repeatedly ? Nobody <nobody@nowhere.com> - 2013-10-28 19:37 +0000
    Re: how to avoid checking the same condition repeatedly ? Neil Cerutti <neilc@norwich.edu> - 2013-10-29 13:44 +0000
      Re: how to avoid checking the same condition repeatedly ? rusi <rustompmody@gmail.com> - 2013-10-29 09:13 -0700
  Re: how to avoid checking the same condition repeatedly ? Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-28 23:14 -0700
    Re: how to avoid checking the same condition repeatedly ? Chris Angelico <rosuav@gmail.com> - 2013-10-29 17:39 +1100
  Re: how to avoid checking the same condition repeatedly ? Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-28 23:53 -0700
    Re: how to avoid checking the same condition repeatedly ? Chris Angelico <rosuav@gmail.com> - 2013-10-29 18:09 +1100
  Re: how to avoid checking the same condition repeatedly ? Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-10-29 16:55 -0700
  Re: how to avoid checking the same condition repeatedly ? alex23 <wuwei23@gmail.com> - 2013-10-30 11:38 +1000
  Re: how to avoid checking the same condition repeatedly ? Mariano Anaya <marianoanaya@gmail.com> - 2013-10-30 06:34 -0700

csiph-web