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


Groups > comp.lang.python > #60214

Re: Method chaining

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.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.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'else:': 0.03; 'none:': 0.07; '22,': 0.09; 'obj': 0.09; 'def': 0.12; 'cleanly': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inclined': 0.16; 'none.': 0.16; 'return,': 0.16; 'wrote:': 0.18; "python's": 0.19; 'work,': 0.20; 'fairly': 0.24; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'piece': 0.31; 'steven': 0.31; 'though.': 0.31; 'fri,': 0.33; 'limitation': 0.33; "i'd": 0.34; 'could': 0.34; 'received:google.com': 0.35; 'false': 0.36; 'done': 0.36; 'method': 0.36; 'list': 0.37; 'convention': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'manually': 0.60; 'identify': 0.61; 'mentioned': 0.61; 'more': 0.64; 'chance': 0.65; '2013': 0.98
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:to :content-type; bh=zihLLSST6zSvia4dbuQnLWxZ/jDiTBAD92YdgDKyQSQ=; b=K7Itd5Ihr5Km6ugW/BOii1DrmozOmy2K1ZhNcjoezyU9p5gkiHTbC7C98s4DlZATw/ 9sXExkroLUxEUTCAw+Hbck5Yw+GNy/pk1jlLLOprAney/GWL2dM29NAM2M2BwCzt0tBB xtMpvbKDgnJ/JXGR/97y6WTbgAVgGKDyCZT7ILrwr7zMip3uE9TWWV2eBmTcBIqrE52F QvFJAWzcfw+6IGyo0CkCTfRPJPqOxTSWLj8n817FIC+ZFe/LCabcKA55nCCpO6o7V7qG 0IbUljH3lCt3kVfzC8IdLKmHzHOl4g+IgZCdNpNbI7u2HTcVre8muun46oLswIVyCAJ3 p3Hw==
MIME-Version 1.0
X-Received by 10.68.222.225 with SMTP id qp1mr11379927pbc.41.1385120649275; Fri, 22 Nov 2013 03:44:09 -0800 (PST)
In-Reply-To <528f3f4e$0$29992$c3e8da3$5496439d@news.astraweb.com>
References <528f3f4e$0$29992$c3e8da3$5496439d@news.astraweb.com>
Date Fri, 22 Nov 2013 22:44:08 +1100
Subject Re: Method chaining
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.3033.1385120652.18130.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385120652 news.xs4all.nl 15893 [2001:888:2000:d::a6]:40512
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60214

Show key headers only | View raw


On Fri, Nov 22, 2013 at 10:26 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>         if callable(obj):
>             def selfie(*args, **kw):
>                 # Call the method just for side-effects, return self.
>                 _ = obj(*args, **kw)
>                 return self
>             return selfie
>         else:
>             return obj

Nice piece of magic. One limitation not mentioned is that this
completely destroys the chance to have a method return anything _other
than_ self. Since this is intended for Python's convention of
"mutators return None", I'd be inclined to check for a None return,
though that might still have some false positives.

            def selfie(*args, **kw):
                # Call the method for side-effects, return self if it
returns None.
                _ = obj(*args, **kw)
                if _ is None: return self
                return _
            return selfie

Either that, or manually identify a set of methods to wrap, which
could possibly be done fairly cleanly with a list of names passed to
__init__. That'd be more work, though.

ChrisA

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


Thread

Method chaining Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-22 11:26 +0000
  Re: Method chaining Chris Angelico <rosuav@gmail.com> - 2013-11-22 22:44 +1100
  Re: Method chaining Peter Otten <__peter__@web.de> - 2013-11-22 13:08 +0100
    Re: Method chaining Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-22 14:26 +0000
      Re: Method chaining Peter Otten <__peter__@web.de> - 2013-11-22 16:20 +0100
        Re: Method chaining Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-22 16:26 +0000
          Re: Method chaining Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-11-22 16:52 +0000
          Re: Method chaining Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-11-22 17:55 +0000
      Re: Method chaining Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-11-22 16:38 +0100
      Re: Method chaining Laszlo Nagy <gandalf@shopzeus.com> - 2013-11-23 17:08 +0100
  Re: Method chaining Terry Reedy <tjreedy@udel.edu> - 2013-11-22 07:34 -0500
    Re: Method chaining Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-22 14:30 +0000
  Re: Method chaining Rotwang <sg552@hotmail.co.uk> - 2013-11-23 19:53 +0000
    Re: Method chaining Rotwang <sg552@hotmail.co.uk> - 2013-11-24 00:28 +0000
      Re: Method chaining Rotwang <sg552@hotmail.co.uk> - 2013-11-24 00:43 +0000
    Re: Method chaining Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-24 14:27 +0000
      Re: Method chaining Rotwang <sg552@hotmail.co.uk> - 2013-11-24 15:01 +0000

csiph-web