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


Groups > comp.lang.python > #63012

Re: Ifs and assignments

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'operator': 0.03; 'heavily': 0.04; 'float': 0.07; 'suppose': 0.07; '*args,': 0.09; 'exception.': 0.09; 'false.': 0.09; 'propagate': 0.09; 'true)': 0.09; 'variant': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; "(i'm": 0.16; '**kwargs):': 0.16; '__new__': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'lower-case': 0.16; 'name):': 0.16; 'nan': 0.16; 'roy': 0.16; 'wrote:': 0.18; 'library': 0.18; 'basically': 0.19; 'code,': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'aspect': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'spirit': 0.31; "they'll": 0.31; 'class': 0.32; 'call.': 0.33; 'fri,': 0.33; 'actual': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; 'info': 0.35; 'common': 0.35; 'received:google.com': 0.35; 'crazy': 0.36; 'false': 0.36; 'done': 0.36; 'method': 0.36; 'should': 0.36; 'too': 0.37; 'rather': 0.38; 'expect': 0.39; 'sure': 0.39; 'either': 0.39; 'applicable': 0.60; 'expression': 0.60; 'most': 0.60; 'skip:t 30': 0.61; 'full': 0.61; 'details.': 0.61; 'first': 0.61; 'real': 0.63; 'become': 0.64; 'production': 0.68; 'smith': 0.68; 'wish': 0.70; 'online': 0.71; 'now:': 0.74; 'disclosure,': 0.75; 'atomic': 0.84; 'calls,': 0.84; 'can...': 0.84; 'examples.': 0.84; 'x):': 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=c2NoU8gN/rls79Z7hBPokKnSRX7KY1LgAfMgi+KWtmM=; b=0812AyaBcalTwIIHJ4PahFhym3u5B0AKKqJJSbXwrf4xx+i0WtZJZgpRVULmdvG86K YvKaVpbKBJzWc2rgqXNM98LvTa9i6Fb0zkFjhK7ikrtf2bk7gifyj/N6At/7pBpI7j9r hh16tqdlod+S/SCst+FL+mQ5ZGhSyO6E6gOG/+5cDVo18CIWDk61tp0soPm3+C7NtGXw qW6ATKvAXApv+j7Zeq9wsX/jbXjVZmMaX795PAFHURx9Ja8MxoynKulh7V7g4zJHneiN 4TPBnXALL1U9b0sFqJ8anv+4UZJgVzaG8CdQzshu2LD1QvMZSXTOpwUJgqr3Og9siMOF ztHw==
MIME-Version 1.0
X-Received by 10.66.66.234 with SMTP id i10mr48759472pat.127.1388707565876; Thu, 02 Jan 2014 16:06:05 -0800 (PST)
In-Reply-To <roy-8601CA.18361502012014@news.panix.com>
References <52C59FF6.5000607@allsup.co> <52C5BD90.9020609@islandtraining.com> <mailman.4811.1388704420.18130.python-list@python.org> <roy-8601CA.18361502012014@news.panix.com>
Date Fri, 3 Jan 2014 11:06:05 +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.4814.1388708064.18130.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1388708064 news.xs4all.nl 2910 [2001:888:2000:d::a6]:53341
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63012

Show key headers only | View raw


On Fri, Jan 3, 2014 at 10:36 AM, Roy Smith <roy@panix.com> wrote:
> The most common place I wish for an atomic "test and assign" is with
> regexes, as in your examples.  This would be so much nicer than what we
> have to do now:
>
>     if re.match(string) as m:
>         print m.group(0)

Here's a crazy idea. Suppose we have a "sticky falseness" that can
quietly propagate through an expression the way a NaN can... then we
could just float that right through the .group() call.

class truth:
    def __new__(cls, x):
        if x: return x
        return object.__new__(cls)
    def __bool__(self):
        return False
    def __getattr__(self, name):
        return self
    def __call__(self, *args, **kwargs):
        return self
    def __repr__(self):
        return repr(False)

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
)

(I'm not sure if I'm using __new__ correctly; I've never actually done
it in production code, and the info I found online was mainly Py2
examples. Should that be done with super(), or is that applicable only
once there's an actual instance with a real MRO?)

I've given the class a lower-case first letter as I'm basically using
this as a function. If this were ever to be considered for the
standard library I would expect to see that aspect of it heavily
bikeshedded, so I'm not too concerned about the details.

In a spirit of full disclosure, I must confess that the idea came from
Pike, where there's a variant member-lookup operator that will happily
return false from false rather than throwing an exception. :)

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