[Python-ideas] Method chaining notation

Chris Angelico rosuav at gmail.com
Mon Feb 24 16:48:17 CET 2014


On Tue, Feb 25, 2014 at 2:16 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Mon, Feb 24, 2014 at 09:01:07AM -0600, Ron Adam wrote:
>
>> 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)
>>
>>
>> Normally .update returns None.  The reason for that is so that it's clear
>> you are mutating an object instead of creating a new one.
>>
>> By using .=, it can return self, but still maintain the clarity between
>> mutation and non-mutation.
>
> How does the update method know whether it is being called via . or via
> .= ? I'm trying to understand how you think this is supposed to work,
> and not having much success. Can you give a sketch of how this .= thingy
> is supposed to operate?

I don't know how his plan is, but mine was for the function to
continue to return None, or 42, or "spam", or whatever it likes, and
for that to be ignored. The expression result would be the initial
object, and the actual function return value is discarded. Function
doesn't need any rewriting.

ChrisA


More information about the Python-ideas mailing list