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


Groups > comp.lang.python > #63011

Re: Ifs and assignments

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'string': 0.09; 'false,': 0.09; 'false.': 0.09; 'happen,': 0.09; 'oh,': 0.09; 'true)': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'notation': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "they'll": 0.31; 'probably': 0.32; 'fri,': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'method': 0.36; 'possible': 0.36; 'either': 0.39; 'skip:t 30': 0.61; 'simple': 0.61; 'become': 0.64; 'calls,': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=SlxhZjYf2D7LuSfv4NCqTk9WgyqoI2AzE5ZSZDPZNu0=; b=bH93QE3JoLp74Q3z4EhjRo8L32bZ/V+azP8mxyTEq9qJYorHYWn7FU0qvdNJp5YtCM hXrO7e305vIOxut0rD1Lijj/AmWYTYYtmdF8iuCE0KxF8hobLBMs5AyaTH4XZeVjQSFD 6r/Cd7WyrAvVuQktid8sN1FUBBa76XNSWZH2tPkscgEWurhOUVdM53OtVl+IvNMBvXj0 3iYqEa2AMP1jqIvNFj6npGjGUUmoOG568I4gRS63th+ahhA3pYb+r3R14J4W9DyL8VK2 CaqFfjYTzpKZgW0GDqvIdCz3ChPKgvaTcZGf0uUAQSP0FY/R0fMn6gM3TfVevwCmapK7 xWiA==
MIME-Version 1.0
X-Received by 10.68.221.233 with SMTP id qh9mr91485755pbc.103.1388707678798; Thu, 02 Jan 2014 16:07:58 -0800 (PST)
In-Reply-To <CAPTjJmoA130Q4+2PHLWiPp1FNaB+c6HgUTWpst4Z_qL3qqoR2w@mail.gmail.com>
References <52C59FF6.5000607@allsup.co> <52C5BD90.9020609@islandtraining.com> <mailman.4811.1388704420.18130.python-list@python.org> <roy-8601CA.18361502012014@news.panix.com> <CAPTjJmoA130Q4+2PHLWiPp1FNaB+c6HgUTWpst4Z_qL3qqoR2w@mail.gmail.com>
Date Fri, 3 Jan 2014 11:07:58 +1100
Subject Re: Ifs and assignments
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.4813.1388707688.18130.python-list@python.org> (permalink)
Lines 18
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1388707688 news.xs4all.nl 2886 [2001:888:2000:d::a6]:50774
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63011

Show key headers only | View raw


On Fri, Jan 3, 2014 at 11:06 AM, Chris Angelico <rosuav@gmail.com> wrote:
> Pass any object through truth() and it'll either stay the same (if
> it's true) or become this object (if it's false). You can then carry
> on with other method calls, and they'll all happily return false.
>
> result = (
>     truth(re1.match(string)).group(0) or
>     truth(re2.match(string)).group(0) or
>     truth(re3.match(string)).group(0) or
>     default_value
> )

Oh, just thought of a possible snag. Since an empty string comes up as
false, this exact notation would fail if the re can match nothing. But
you probably know if it's possible for that to happen, so don't use
this simple short-hand. :)

ChrisA

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


Thread

Re: Ifs and assignments John Allsup <pydev@allsup.co> - 2014-01-02 21:44 +0000
  Re: Ifs and assignments Roy Smith <roy@panix.com> - 2014-01-02 18:36 -0500
    Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 11:07 +1100
    Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 11:06 +1100
    Re: Ifs and assignments Ethan Furman <ethan@stoneleaf.us> - 2014-01-02 20:16 -0800
    Re: Ifs and assignments Chris Angelico <rosuav@gmail.com> - 2014-01-03 16:35 +1100

csiph-web