Method chaining

Steven D'Aprano steve+comp.lang.python at
Fri Nov 22 17:26:06 CET 2013

On Fri, 22 Nov 2013 16:20:03 +0100, Peter Otten wrote:

> Steven D'Aprano wrote:
>> On Fri, 22 Nov 2013 13:08:03 +0100, Peter Otten wrote:
>>> These things are nice to write as long as you omit the gory details,
>>> but personally I don't want to see the style it favours in my or other
>>> people's code.
>> There's not really a lot of difference
> That cuts both ways ;)

Actually, I was wrong. See below.

>> between:
>>     obj = MyClass()
>>     obj.spam()
>>     obj.eggs()
>>     obj.cheese()
>> and
>>     obj = MyClass().spam().eggs().cheese()
>> except the first takes up a lot more vertical space.
> I've not yet run short of vertical space ;)

However, here is a real difference:

# With chaining
thing = func(MyClass().spam().eggs().cheese(), 


# Without chaining
temp1 = MyClass()
temp2 = MyClass()
temp3 = OtherClass()
thing = func(temp1, temp2, temp3)

In this case the chained version doesn't obscure the intention of the 
code anywhere near as much as the unchained version and its plethora of 
temporary variables.

>> Chained method calls is idiomatic in some languages.
> Languages with mutable objects?

Yes. It's a "Design Pattern" applicable to any language with mutator 
methods. Here are three examples in C#, Java and Ruby:

although in fairness I wouldn't call it idiomatic in C# or Java.

Ruby 1.9 even added a new method to Object, tap, specifically to allow 
chaining of methods, which itself was copied from Ruby-On-Rails' 
"returning" helper. So I think it's fair to say that method chaining
for mutation is idiomatic in Ruby.

This idea goes back to Smalltalk, and is essentially just a 
pipeline. Hardly something weird.

(Technically, if the methods return self rather than None, it's method 
cascading. Chaining is any sequence of method calls, whether they return
self or something else.)

Dart includes syntax for method cascades, which I'm told looks like 

x = SomeClass()

The technique also comes with the blessing of Martin Fowler, where it
is an important part of fluent interfaces:


"The common convention in the curly brace world is that modifier 
methods are void, which I like because it follows the principle 
of CommandQuerySeparation. This convention does get in the way of 
a fluent interface, so I'm inclined to suspend the convention for 
this case."

And last but not least, if you want to go all the way down to the lambda 
calculus and combinator theory, my "selfie" adapter function is just a 
form of the K-combinator (a.k.a. the Kestrel). So there's a deep and 
powerful mathematical pedigree to the idea, if that matters.


More information about the Python-list mailing list