[Python-ideas] Method chaining notation

Terry Reedy tjreedy at udel.edu
Fri Feb 21 22:37:07 CET 2014


On 2/21/2014 12:30 PM, Chris Angelico wrote:

> It's common in languages like C++ to return *this by reference if
> there's nothing else useful to return. It's convenient, it doesn't
> cost anything much, and it allows method chaining. The Python
> convention, on the other hand, is to return self only if there's a
> very good reason to,

Off the top of my head, I cannot think of any methods that return self. 
(If there are some, someone please remind or inform me.)

 > and to return None any time there's mutation that
> could plausibly return a new object of the same type (compare
> list.sort() vs sorted()).

The rule for mutation methods is to return None unless there is 
something *other than* self to return, like list.pop and similar remove 
(mutate) and return methods. List.sort and list.reverse returned None 
from the beginning, long before sorted and reversed were added.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list