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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Fri, Nov 22, 2013 at 10:26 PM, Steven D'Aprano 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