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 that design patterns were all the thing. Not so much now. But Python - does it have design patterns?

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:

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 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.

  • Mike James is the author of Programmer's Python: Something Completely Different series of books which sets out to show how Python diverges from other programming languages and how its special features deserve our attention. It's second volume Everything Is Data was published last month.

For recommendations of Python books see Books for Pythonistas and Python Books For Beginners in our Programmer's Bookshelf section.



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


Computer Graphics from Scratch (No Starch Press)

Author: Gabriel Gambetta
Publisher: No Starch Press
Pages: 248
ISBN: 978-1718500761
Print: 1718500769
Kindle: B085BVJG5B
Audience: People interested in creating 3D graphics
Rating: 5
Reviewer: Kay Ewbank

This is a well written book that explains the topic of 3D rendering, and tries its hardest to mi [ ... ]

Discovering Modern C++, 2nd Ed

Author: Peter Gottschling
Publisher: Addison-Wesley
Pages: 576
ISBN: 978-0136677642
Print: 0136677649
Kindle: ‎ B09HTJRJ3V
Audience: C++ developers
Rating: 5
Reviewer: Mike James

Modern C++ who would want to write anything else? Is this a suitable introduction for the rest of us?

More Reviews



Last Updated ( Wednesday, 08 June 2022 )