Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60246
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'expressions': 0.07; 'obj': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'wrapped': 0.09; 'def': 0.12; 'assignments.': 0.16; 'chained': 0.16; 'fly': 0.16; 'message- id:@post.gmane.org': 0.16; 'mutable': 0.16; 'name)': 0.16; 'name):': 0.16; 'received:80.91.229.3': 0.16; 'received:mediaways.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:pool.mediaways.net': 0.16; 'self.obj': 0.16; 'skip:[ 30': 0.16; 'skip:f 30': 0.19; 'seems': 0.21; 'coding': 0.22; 'header:User-Agent:1': 0.23; '(or': 0.24; 'define': 0.26; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; "d'aprano": 0.31; 'steven': 0.31; 'writes:': 0.31; 'class': 0.32; 'lists': 0.32; 'probably': 0.32; 'another': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'could': 0.34; "can't": 0.35; 'agree': 0.35; 'objects': 0.35; 'in:': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:o 30': 0.61; 'such': 0.63; 'temporary': 0.65; 'received:93': 0.72; 'localized': 0.84; 'object:': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
| Subject | Re: Method chaining |
| Date | Fri, 22 Nov 2013 17:55:45 +0000 (UTC) |
| References | <528f3f4e$0$29992$c3e8da3$5496439d@news.astraweb.com> <mailman.3035.1385122060.18130.python-list@python.org> <528f6989$0$29992$c3e8da3$5496439d@news.astraweb.com> <mailman.3044.1385133587.18130.python-list@python.org> <528f859e$0$29992$c3e8da3$5496439d@news.astraweb.com> <loom.20131122T174819-384@post.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | sea.gmane.org |
| User-Agent | Loom/3.14 (http://gmane.org/) |
| X-Loom-IP | 93.132.229.124 (Mozilla/5.0 (Windows NT 6.2; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0) |
| 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.3051.1385142973.18130.python-list@python.org> (permalink) |
| Lines | 73 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1385142973 news.xs4all.nl 15994 [2001:888:2000:d::a6]:45860 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:60246 |
Show key headers only | View raw
Wolfgang Maier <wolfgang.maier <at> biologie.uni-freiburg.de> writes:
>
> Steven D'Aprano <steve+comp.lang.python <at> pearwood.info> writes:
>
> > # With chaining
> > thing = func(MyClass().spam().eggs().cheese(),
> > MyClass().aardvark(),
> > OtherClass().fe().fi().fo().fum(),
> > )
> > do_stuff_with(thing)
> >
> > versus:
> >
> > # Without chaining
> > temp1 = MyClass()
> > temp1.spam()
> > temp1.eggs()
> > temp1.cheese()
> > temp2 = MyClass()
> > temp2.aardvark()
> > temp3 = OtherClass()
> > temp3.fe()
> > temp3.fi()
> > temp3.fo()
> > temp3.fum()
> > thing = func(temp1, temp2, temp3)
> > do_stuff_with(thing)
> >
>
> Another use case might be in comprehensions and generator expressions ??
>
> thing = [MyClass().spam().eggs(x).cheese() for x in sequence]
>
> where you can't use all those temporary assignments.
>
> Best,
> Wolfgang
>
>
Thinking about this, you could define __call__ for your chained class to
return the wrapped object:
class chained:
def __init__(self, obj):
self.obj = obj
def __repr__(self):
return repr(self.obj)
def __getattr__(self, name):
obj = getattr(self.obj, name)
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
def __call__(self):
return self.obj
This would encourage its localized use on the fly as in:
thing = [MyClass().spam().eggs(x).cheese()() for x in sequence]
where the intention probably would be to generate a list of lists (or other
mutable objects), not of objects of class chained. Localizing the use of
chained also seems like a good idea to me as I do share Peter's worries
about coding style, while I also agree with you that such a class comes in
handy from time to time.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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