Computing Reviews
Today's Issue Hot Topics Search Browse Recommended My Account Log In
Review Help
Search
Practical Python design patterns : Pythonic solutions to common problems
Badenhorst W., Apress, New York, NY, 2017. 350 pp. Type: Book (978-1-484226-79-7)
Date Reviewed: Apr 12 2018

Python programmers (as well as programmers in other languages) use patterns all the time. For example, the Python statement “for character in string” is a pattern for traversing a string. One of the strengths of Python is that it has a pattern for traversing a string, whereas some other languages force the programmer to coerce a for or while loop to traverse a string, often leading to boundary errors. (Please note, examples of boundary errors are starting with the second character in the string rather than the first or failing to stop at the end of the string.) Further credit goes to the designer of the language for generalizing this pattern to “for item in data structure” so that the same pattern works for lists, tuples, sets, and dictionaries. The programmer who thinks in terms of patterns writes better code and is more productive than the programmer who thinks in terms of lines of code. But this pattern is a pattern at the code level. What if it were possible to take a step back and create the same economy of cognition and expression at a slightly higher level, say the program level rather than the code level? Wouldn’t that lead to better programs being written by more productive programmers? The answer, of course, is yes. And that is what this book is about.

I often tell my database class students that there are only 12 patterns, and once they have learned all 12 they only need to figure out which pattern solves a particular problem. I have never bothered to sketch out the 12 patterns. I picked 12 because I liked the number. There may really be 10, or 14, or 20, but not hundreds. The author of this book has gone to the trouble to name and sketch out 19 patterns for Python programs. Each pattern is supported with code, explanations of why using the pattern is better than not using a pattern, and even some techno-folk wisdom such as, “Favor object composition over inheritance.” This homily may rankle object purists. But, the truth is that inheritance (having objects structured into neat taxonomies) is more important to the people who create objects for others to use, while composition (building things out of reusable parts) is more important to people who create and maintain executable programs.

There are too many patterns to explain each one, but an example is in order to reveal the nature of the book. The example is the factory pattern. (This is going to get a little technical, but that cannot be helped.) Say you are creating a graphics program to draw shapes on a screen. (The author actually uses a game example with Pygame, but this is simpler.) You have to create objects such as a square, a circle, a triangle, and so forth. Instead of creating the geometric object directly, you invoke the factory object and pass it the name of the figure (for example, shape.factory(“circle”)). It then returns an object of type circle. This provides several benefits. First, it provides a level of independence between the object creator and the object user. A parallelogram could be implemented directly or as four triangles. The designer is free to try different implementations without affecting the class user who simply says shape.factory(“parallelogram”) and doesn’t worry about how it is implemented or if the implementation changes. Second, if the geometric object allows parameters, the factory can provide defaults if the user did not provide them. Third, if new geometric objects are added, they only need to be added to factory and they will be available to the user. Finally, this provides a level of conceptual as well as program independence. The programmer can think of the factory and the things it can create rather than having to remember all of the geometric objects.

But it gets better. One might have other kinds of objects such as user interface objects like menus, lists, checkbox sets, and so on. These may be contained in a graphical user interface (GUI) factory. What if you get too many factories? Well, you can create an abstract factory (another pattern) and invoke it to invoke the specific object constructor. This way, the user does not have to remember if the box object is in the geometric patterns factory or the GUI factory. And this is only one of 19 patterns.

This book is not for beginners. If you consider yourself a fairly competent Python programmer and want to become more proficient, then this book will help you do that. As a textbook, this would be appropriate for an advanced programming class for students who have had one or two previous courses and know how to create objects. In the latter case, the instructor would have to provide a little theory, as the book focuses more on the practical aspects of programming.

More reviews about this item: Amazon

Reviewer:  J. M. Artz Review #: CR145969 (1807-0359)
Bookmark and Share
  Reviewer Selected
Featured Reviewer
 
 
Patterns (D.3.3 ... )
 
 
Python (D.3.2 ... )
 
Would you recommend this review?
yes
no
Other reviews under "Patterns": Date
Java design patterns: a tutorial
Cooper J., Addison-Wesley Longman Publishing Co., Inc., Boston, MA, 2000.  329, Type: Book (9780201485394)
Mar 1 2000
Open pattern matching for C++
Solodkyy Y., Dos Reis G., Stroustrup B.  GPCE 2013 (Proceedings of the 12th International Conference on Generative Programming: Concepts & Experiences, Indianapolis, IN, Oct 27-28, 2013)33-42, 2013. Type: Proceedings
Jan 29 2014
MapReduce design patterns: building effective algorithms and analytics for Hadoop and other systems
Miner D., Shook A., O’Reilly Media, Inc., Sebastopol, CA, 2013.  230, Type: Book (978-1-449327-17-0)
Aug 22 2014
more...

E-Mail This Printer-Friendly
Send Your Comments
Contact Us
Reproduction in whole or in part without permission is prohibited.   Copyright 1999-2024 ThinkLoud®
Terms of Use
| Privacy Policy