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.

Banner

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:
      print("float")
      return a+b  
  def addNums(self,a:float,b:Str)->float:
      print("string")
      return a+float(b)sumr=Summer()

print(sumr.addNums(1.0,2.0))
print(sumr.addNums(1.0,"2.0"))

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.

Banner


SQL Server Advanced Troubleshooting and Performance Tuning (O'Reilly)

Author: Dmitri Korotkevitch
Publisher: O'Reilly
Pages: 497
ISBN: 978-1098101923
Print:1098101928
Kindle: B0B197NYD7
Audience: DBAs & database devs
Rating: 5
Reviewer: Ian Stirk

This book aims to improve the performance of your SQL Servers, how does it fare?



Learn dbatools in a Month of Lunches

Author: Chrissy LeMaire et al
Publisher: Manning
Pages: 400
ISBN: 978-1617296703
Print: 1617296708
Kindle: B0B39PCHL8
Audience: SQL Server DBAs
Rating: 5
Reviewer: Ian Stirk 

This book aims to make it easier to manage your SQL Server estate, how does it fare? 


More Reviews

<ASIN:0201633612>

<ASIN:1871962595>

Last Updated ( Wednesday, 08 June 2022 )