October 2018
Beginner to intermediate
466 pages
12h 2m
English
The preceding canonical implementation of the strategy pattern, while very common in most object-oriented libraries, is rarely seen in Python programming.
These classes each represent objects that do nothing but provide a single function. We could just as easily call that function __call__ and make the object callable directly. Since there is no other data associated with the object, we need do no more than create a set of top-level functions and pass them around as our strategies instead.
Opponents of design pattern philosophy will therefore say, because Python has first-class functions, the strategy pattern is unnecessary. In truth, Python's first-class functions allow us to implement the strategy pattern in a more straightforward ...