Python programming with design patterns

Author: James W. Cooper
Publisher: Addison Wesley
Date: February 2022
Pages: 352
ISBN: 978-0137579938
Print: 0137579934
Kindle: B09D2RKQB5
Audience: Python developers
Rating: 1
Reviewer: Mike James
There was a time when design patterns were everything. Not so much now. But does Python – have any design patterns?

The now classic Gang of Four design pattern book Design Patterns: Object-Oriented Reusable Software Elements by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides was a revelation to anyone who read it for the first time and still is – only a little less as many ideas have passed into the mainstream consciousness. The book used C++ and SmallTalk and it has been rewritten in other languages ​​several times and 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 aggressively object-oriented. Some of them make little sense in a less strictly typed, and perhaps less committed, object-oriented language. Python is not strongly typed as shown in Programmer’s Python: Everything is an Object where Python’s approach to object-oriented programming is both relaxed and resourceful. It offers the best of object-oriented programming without making the programmer jump through strictly typed and class-based hoops.

This particular book is an attempt to express classic design patterns in Python – something I would take a deep breath before doing. The problem is that Python isn’t C++, or any other classic OOP language, and you’d need to think very hard about how to get the job done – just copying won’t produce a Pythonic version. You need an author who knows Python in depth and is committed to their worldview. A 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 benefit from the language.

I started reading this book hoping to learn something, but quickly became very apprehensive. I started to worry in Chapter 1, which is an introduction to Python. Yes, it describes basic Python – but it doesn’t indicate any of the ways Python differs from C++ and other object oriented languages ​​and some of those are really important. There is no mention that classes are objects, functions are objects, inheritance is dynamic, variables are not types, typing is dynamic and not exclusively class-based, etc. I also found many feature descriptions naive and almost silly – but maybe this is just a simplification out of consideration for the newbie so I kept hoping.

Then I got to a section that describes typing in Python and found that the author seems to believe that Python really is strongly typed if you just try hard enough. The example shown demonstrates how Python supports function overloading by signature. The example is:

Class Summer():
  def addNums(self,a:float,b:float)->float:
      return a+b  
  def addNums(self,a:float,b:Str)->float:
      return a+float(b)sumr=Summer()


The explanation is that Python selects which version of addNums to call depending on the argument types. That’s not how it works. Python does not support types, let alone typed signatures. When addNums is declared for the second time, it just overrides the first definition, and the two function calls both call the second version of the function. This seems to work because Python automatically converts the string to a valid float using an auto-conversion function. It still “works” if you delete the first function definition. Misunderstanding 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 were another language.

From this embarrassing introduction to Python, we have an introduction to tkinter and GUI programming. No discussion of events or event handling makes this uninformative. From there we thought about the basic design patterns – factory, factory method, abstract factory, etc. Everything was fine until I reached the singletons section. I know a few ways to implement a singleton in Python and if you search the web you will find a few. Chapter 8 presents a very simple and straightforward method of doing the job, but it makes it seem like it was the only way. It doesn’t discuss pros or cons and further describes how it works as if it were a built-in Python feature:

“you may not know about static method and __instance variable”

Indeed, you might not – because neither of these exist implicitly. The __example variable is just a variable that happens to have been called __instance. It’s nothing special, other than it’s a private variable with a mangled name, but that’s not mentioned. and “the static method” is actually a static decorator function that converts any method to a static method, not just one as shown.

At this point, I gave up. I really couldn’t bother to read the rest as it would have just been an exercise in finding misunderstandings and errors. As you might guess, I can’t recommend this book unless you enjoy finding faults – and believe it or not, I don’t.

If you want to know more about design patterns, read the original. It is still up to date.

  • Mike James is the author of Programmer’s Python: Something Completely Different series of books that proposes to show how Python deviates from other programming languages ​​and how its peculiarities deserve our attention. Its second volume, Everything Is Data, was released last month.

For Python book recommendations, see Books for Pythonistas and Python books for beginners in our Programmer’s library section.

To keep up with our coverage of books for programmers, follow @bookwatchiprog on Twitter or subscribe to the I Programmer’s Books RSS feed for new additions to Book Watch and for new reviews.

Abdul J. Gaspar