Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #58038
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-10-30 06:34 -0700 |
| References | <mailman.1693.1382953842.18130.python-list@python.org> |
| Message-ID | <0af54775-939e-4b3e-ac38-cd153e7d315c@googlegroups.com> (permalink) |
| Subject | Re: how to avoid checking the same condition repeatedly ? |
| From | Mariano Anaya <marianoanaya@gmail.com> |
On Monday, October 28, 2013 6:50:19 AM UTC-3, Wolfgang Maier wrote:
> Dear all,
>
> this is a recurring programming problem that I'm just not sure how to solve
>
> optimally, so I thought I'd ask for your advice:
>
> 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. Of course, you could rewrite the above as:
>
>
>
> if needs_processing:
>
> for elem in iterable:
>
> pre_process(elem) # reformat elem in place
>
> print(elem)
>
> else:
>
> for elem in iterable:
>
> print(elem)
>
>
>
> but this means unnecessary code-duplication.
>
>
>
> You could also define functions (or class methods):
>
> def pre_process_and_print (item):
>
> pre_process(item)
>
> print(item)
>
>
>
> def raw_print (item):
>
> print(item)
>
>
>
> then:
>
> process = pre_process_and_print if needs_processing else raw_print
>
> for elem in iterable:
>
> process(elem)
>
>
>
> but while this works for the simple example here, it becomes complicated if
>
> pre_process requires more information to do its job because then you will
>
> have to start passing around (potentially lots of) arguments.
>
>
>
> So my question is: is there an agreed-upon generally best way of dealing
>
> with this?
>
>
>
> Thanks for your help,
>
> Wolfgang
Hi All,
Trying to help you out, I wonder if something like the following code will help on what you need.
for elem in (needs_processing and iterable or []):
pre_process(elem) # reformat elem in place
print(elem)
It avoids code duplication and only process and iterates if the condition is True.
I hope it helps, otherwise, we could keep thinking alternatives.
Regards.
Mariano.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll 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