Method chaining

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Nov 22 09:30:23 EST 2013


On Fri, 22 Nov 2013 07:34:53 -0500, Terry Reedy wrote:

> On 11/22/2013 6:26 AM, Steven D'Aprano wrote:
>> A frequently missed feature is the ability to chain method calls:
>>
>> x = []
>> x.append(1).append(2).append(3).reverse().append(4) => x now equals [3,
>> 2, 1, 4]
>>
>>
>> This doesn't work with lists, as the methods return None
> 
> True for the 7 pure mutation methods but not for .copy, .count, .index,
> and .pop. The last both mutates and returns.

Yes, that is correct. In this case, the assumption behind the chained 
adapter is that we don't care about the results of calling those methods, 
we only care about the mutation they cause. If that's not the case, then 
chained() isn't for us.


-- 
Steven



More information about the Python-list mailing list