Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57806
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'subject:same': 0.07; 'e.g.,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'reformat': 0.09; 'rewrite': 0.09; 'def': 0.12; 'advice:': 0.16; 'elem': 0.16; 'iterable,': 0.16; 'iterable:': 0.16; 'message-id:@post.gmane.org': 0.16; 'of)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'elements': 0.16; 'subject: ?': 0.16; 'all,': 0.19; 'passing': 0.19; 'code,': 0.22; 'example': 0.22; 'programming': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'earlier': 0.24; '(or': 0.24; 'question': 0.24; 'define': 0.26; 'somewhere': 0.26; 'header:X-Complaints-To:1': 0.27; 'generally': 0.29; "i'm": 0.30; 'received:132': 0.31; 'class': 0.32; 'becomes': 0.33; 'subject:the': 0.34; "i'd": 0.34; 'could': 0.34; 'problem': 0.35; 'but': 0.35; 'there': 0.35; 'charset:us-ascii': 0.36; 'thanks': 0.36; 'checks': 0.38; 'depends': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'help,': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'solve': 0.60; 'simple': 0.61; "you're": 0.61; 'information': 0.63; 'kind': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'chance': 0.65; 'dear': 0.65; 'as:': 0.81; 'recurring': 0.91; 'imagine': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
| Subject | how to avoid checking the same condition repeatedly ? |
| Date | Mon, 28 Oct 2013 09:50:19 +0000 (UTC) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | sea.gmane.org |
| User-Agent | Loom/3.14 (http://gmane.org/) |
| X-Loom-IP | 132.230.1.27 (Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0) |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1693.1382953842.18130.python-list@python.org> (permalink) |
| Lines | 52 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1382953842 news.xs4all.nl 15922 [2001:888:2000:d::a6]:40608 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:57806 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Next 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