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


Groups > comp.lang.python > #63001

Re: Ifs and assignments

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.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.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'assign': 0.07; 'desired.': 0.07; 'logic': 0.09; 'tests,': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; '-tkc': 0.16; 'conditional': 0.16; 'expression,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'pairs': 0.16; 'simplifies': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'pfxlen:0': 0.19; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'gets': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'usually': 0.31; 'indentation': 0.31; 'running': 0.33; 'something': 0.35; 'there': 0.35; 'done': 0.36; 'charset:us-ascii': 0.36; 'ability': 0.39; 'break': 0.61; 'new': 0.61; 'john': 0.61; 'save': 0.62; 'received:50.22': 0.84
Date Thu, 2 Jan 2014 16:08:23 -0600
From Tim Chase <python.list@tim.thechases.com>
To John Allsup <pydev@allsup.co>
Subject Re: Ifs and assignments
In-Reply-To <52C59FF6.5000607@allsup.co>
References <52C59FF6.5000607@allsup.co>
X-Mailer Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
X-Get-Message-Sender-Via boston.accountservergroup.com: authenticated_id: tim@thechases.com
Cc python-list@python.org
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.4809.1388700455.18130.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1388700455 news.xs4all.nl 2885 [2001:888:2000:d::a6]:39190
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63001

Show key headers only | View raw


On 2014-01-02 17:20, John Allsup wrote:
> m = r1.search(w)
> if m:
> 	handleMatch1(m)
> else:
> 	m = r2.search(w)
> 	if m:
> 		handleMatch2(m)
> 	else:
> 		print("No match")
> 
> if not running unnecessary matches, yet capturing groups in the
> event of a successful match, is what is desired.
> 
> If there are multiple tests, the indentation gets silly.  This
> arises because having removed the ability to assign in an
> expression, there is no way to save the result of a function call
> that is used in a conditional at all.

Usually this is done something like

  pairs = (
  for r, fn in (
      (r1, handleMatch1),
      (r2, handleMatch2),
      (r3, handleMatch3),
      ):
    m = r.search(w)
    if m:
      fn(m)
      break
  else: # yes, a for/else, a handy Python construct if new to you
    print("No match")

which simplifies the logic considerably, avoiding the deep nesting.

-tkc


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


Thread

Re: Ifs and assignments Tim Chase <python.list@tim.thechases.com> - 2014-01-02 16:08 -0600

csiph-web