October 2017
Intermediate to advanced
532 pages
16h 10m
English
To motivate method chaining, let's take a simple English sentence and translate the chain of events into a chain of methods. Consider the sentence, A person drives to the store to buy food, then drives home and prepares, cooks, serves, and eats the food before cleaning the dishes.
A Python version of this sentence might take the following form:
>>> person.drive('store')\ .buy('food')\ .drive('home')\ .prepare('food')\ .cook('food')\ .serve('food')\ .eat('food')\ .cleanup('dishes')
In the preceding code, the person is the object calling each of the methods, just as the person is performing all of the actions in the original sentence. The parameter passed to each of the methods specifies how the method operates.
Although it is ...
Read now
Unlock full access