|Python Programming with Design Patterns|
Author: James W. Cooper
The now classic Gang of Four design patterns book Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides was a revelation to anyone reading it for the first time and still is - only slighly less so as many of the ideas have moved into the general consciousness. The book used C++ and some SmallTalk and it has been rewritten in other languages many times and it has inspired similar books in many languages. The key thing to know, however, is that the design patterns are object-oriented - you could even say strongly and agressively object-oriented. Some of them make little sense in a less strictly typed, and perhaps less comitted, object-oriented langauge. Python isn't strongly typed as argued in Programmer's Python: Everything is an object where Python's approach to object-oriented programming is revealed as both relaxed and ingenious. It delivers the best of object-oriented programming without making the programmer jump though strictly-typed, class-based hoops.
This particular book is an attempt at expressing the classic design patterns in Python - something I would take a very deep breath before doing. The problem is that Python is not C++, or any other classical OOP language, and you would need to think very hard about how to do the job - just copying isn't going to produce a Pythonic version. You need an author who knows Python in depth and is committed to its view of the world. One serious problem is that you can learn to program in Python without ever really understanding its unique approach. In short, you can write Python like C++ or Java or ... , but you will make mistakes and not take advantage of the language.
I started reading this book in hope I would learn something, but I quickly became very worried. I started to worry at Chapter 1, which is an introduction to Python. Yes, it describes basic Python - but it doesn't point out any of the ways in which Python differs from C++ and other object-oriented languages and some of these are really important. There is no mention that classes are objects, functions are objects, inheritance is dynamic, variables aren't types, typing is dynamic and not exclusively class-based and so on.. I also found many of the descriptions of features naive and almost silly - but perhaps this is just a simplification out of consideration for the beginner so I carried on in hope.
Then I got to a section that describes typing in Python and discovered that the author seems to believe that Python really is strongly-typed if only you try hard enough. The presented example explains how Python supports function overloading by signature. The example is:
The explanation is that Python selects which version of addNums to call depending on the types of the arguments. This is not how it works. Python doesn't support types, let alone typed signatures. When addNums is declared for the second time it simply replaces the first define and the two function calls both call the second version of the function. It appears to work because Python automatically converts the string into an valid float using an automatic conversion function. It still "works" if you delete the first function definition. To misunderstand the language at this deep level doesn't bode well for anything to do with design patterns and their relationship to Python's approach to objects and type. This is a clear indication that we are not reading a book about Python, but about Python as if it was another language.
From this embarrassing introduction to Python we have an introduction to tkinter and GUI programming. No discussion of events or event handling make this uninformative. From here we plod thought the basic design patterns - Factory, Factory method, abstract Factory and so on. All was well until I reached the section on singletons. I know a few ways to implement a singleton in Python and if you do a web search you will find a few. Chapter 8 introduces a very simple and straightforward method of doing the job but it makes it sound as if it it was the only way. It doesn't discuss any pros or cons and in addition it describes the way that it works as if it was a built-in Python feature:
"you might not be aware of the static method and the __instance variable"
Indeed you might not - because neither exist in the way implied. The __instance variable is just a variable that happens to have been called __instance. It's nothing special, apart from being a name-mangled private variable, but this isn't mentioned. and "the static method" is in fact a static decorator function which converts any method into a static method, not just one as stated.
At this point I gave up. I really couldn't be bothered to read the rest as it would have just been an exercise in finding the misunderstandings and errors. As you can guess I can't recommend this book unless you delight in finding fault - and believe it or not I don't.
If you want to find out about design patterns then read the original. It is still relevant.
|Last Updated ( Wednesday, 08 June 2022 )|