Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'languages.': 0.04; 'explicit': 0.07; 'obj': 0.09; 'omit': 0.09; "people's": 0.09; 'profits': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'chained': 0.16; 'distinct': 0.16; 'mutable': 0.16; 'objects.': 0.16; 'objects?': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'header:User-Agent:1': 0.23; 'space.': 0.24; 'initial': 0.24; "i've": 0.25; 'header:X-Complaints-To:1': 0.27; "doesn't": 0.30; 'code': 0.31; '+0100,': 0.31; "d'aprano": 0.31; 'follows': 0.31; 'minor': 0.31; 'steven': 0.31; 'vertical': 0.31; 'class': 0.32; 'languages': 0.32; 'skip:m 30': 0.32; 'run': 0.32; 'fri,': 0.33; 'style': 0.33; 'actual': 0.34; "i'd": 0.34; 'problem': 0.35; 'except': 0.35; 'but': 0.35; 'building': 0.35; 'there': 0.35; 'really': 0.36; 'sequence': 0.36; 'method': 0.36; 'clear': 0.37; 'being': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'short': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'space': 0.40; 'first': 0.61; 'more': 0.64; 'series': 0.66; 'details,': 0.68; 'default': 0.69; 'cuts': 0.84; 'otten': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Method chaining Date: Fri, 22 Nov 2013 16:20:03 +0100 Organization: None References: <528f3f4e$0$29992$c3e8da3$5496439d@news.astraweb.com> <528f6989$0$29992$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084aa86.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385133587 news.xs4all.nl 15904 [2001:888:2000:d::a6]:53026 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60235 Steven D'Aprano wrote: > On Fri, 22 Nov 2013 13:08:03 +0100, Peter Otten wrote: > >> These things are nice to write as long as you omit the gory details, but >> personally I don't want to see the style it favours in my or other >> people's code. > > There's not really a lot of difference That cuts both ways ;) > between: > > obj = MyClass() > obj.spam() > obj.eggs() > obj.cheese() > > and > > obj = MyClass().spam().eggs().cheese() > > > except the first takes up a lot more vertical space. I've not yet run short of vertical space ;) > Chained method calls is idiomatic in some languages. Languages with mutable objects? > If there is a problem with it, it is that > it doesn't make it clear that each method call is being used only for its > side-effects, rather than it being a series of distinct objects. But in > my opinion that flaw is a very minor one. > > The nice thing about using an explicit method chaining call rather than > building your class to support it by default is that the initial call to > the adaptor signals that everything that follows is called only for the > side-effects. > > obj = chained(MyClass()).spam().eggs().cheese() obj = MyClass(); obj.spam(); obj.eggs(); obj.cheese() OK, that one is disgusting... Anyway, I'd like to see a sequence of method names taken from actual code that profits from this chaining pattern.