[Python-ideas] Method chaining notation

Stephen J. Turnbull stephen at xemacs.org
Wed Feb 26 08:19:21 CET 2014


Ron Adam writes:

 > >   >     def names(defaults, pos_names, pos_args, kwds):
 > >   >         return  {}.=update(defaults) \
 > >   >                   .=update(zip(pos_names, pos_args) \
 > >   >                   .=update(kwds)

 > >      def names(defaults, pos_names, pos_args, kwds):
 > >          for dct in pos_names, pos_args, kwds:
 > >              defaults.update(dct)
 > >          return defaults
 > 
 > Not quite the same but close.  I just tried to come up with a more
 > realistic example without having to look up a lot code.  How does
 > pos_args in your example get paired with names?

Sorry, I knew that before dinner but forgot after dinner.  Same way as
in yours:

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

If that doesn't work in my version (I've never used zip that way), how
does it work in yours?  BTW, I'd actually be more likely to write that
now as

    def names(defaults, *updates):
        for update in updates:
            defaults.update(update)
        return defaults

and call it with "names(zip(pos_names, pos_args), kwds)".



More information about the Python-ideas mailing list