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


Groups > comp.lang.python > #60214

Re: Method chaining

References <528f3f4e$0$29992$c3e8da3$5496439d@news.astraweb.com>
Date 2013-11-22 22:44 +1100
Subject Re: Method chaining
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3033.1385120652.18130.python-list@python.org> (permalink)

Show all headers | 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