[Python-ideas] Method chaining notation

Stephen J. Turnbull stephen at xemacs.org
Tue Feb 25 09:21:00 CET 2014


Ron Adam writes:

 > You would probably see it used more often like this...
 > 
 >     def names(defaults, pos_names, pos_args, kwds):
 >         return  {}.=update(defaults) \
 >                   .=update(zip(pos_names, pos_args) \
 >                   .=update(kwds)

I actually have a bunch of code in one of my apps that implements the
same thing for a different reason (cascading configs), but my
implementation is

    def names(defaults, pos_names, pos_args, kwds):
        for dct in pos_names, pos_args, kwds:
            defaults.update(dct)
        return defaults

The other obvious use for this (as several have posted) is
accumulating a sequence.  In which case most uses will be well-handled
with a genexp, or if you need a concrete sequence, a listcomp, and the
body becomes a one (logical) liner (although it will very likely be
formatted in multiple lines).



More information about the Python-ideas mailing list