Python is one of the most widely used programming languages in the world. It is used everywhere from primary school education to workaday web development, to the most advanced scientific research institutes of the world. However, like all programming languages, Python has a collection of "Pythonic" ways of accomplishing tasks that are easy to overlook, especially when habits are borrowed wholesale from work in other programming languages. Better Python Code is a guide to Pythonic programming. The book presents common mistakes that Python developers make--even Python developers who have used the language for years--often because Python sometimes presents "attractive nuisances." Throughout, the book is a guide to better programming in the core Python language. Each section shows a concrete but concise example of some misunderstanding or bad habit in action. Each section contains a description of what is wrong with the sample code and a suggestion for one or more better ways to code equivalent functionality without the initial pitfall. Every pitfall addressed in this book reflects foibles, errors, and misunderstandings that the author as seen in concrete, widely used code bases written by experienced developers, over his 25 years of writing Python. Both beginners and developers with decades of experience will learn to correct limitations in the code they write after reflecting on these discussions.
This book has great utility for anyone who is coding in Python.
Pragmatism rules this text. While I really enjoy Mertz' writing, especially the diversions on history and vocabulary, this work is concise and very much to the point of writing code that will run efficiently and be readable and maintainable as well. Although antipatterns and error prone code are presented as "mistakes", Mertz avoids talking down to the user in any kind of smug, pejorative, or harshly prescriptive manner. Rather, many of the mistakes he confesses to having done himself. As a trainer and consultant, he has reviewed a lot of code; at one point in the book, he reemphasizes that the mistakes are actual patterns he has seen repeatedly in code.
Some highlights:
- new features in Python 3.x releases that make code faster, simpler, easier to write, and more readable.
- a good introduction and treatment of numeric data and all the associated pitfalls with some useful bit level depth analysis of floating point numbers to aid understanding.
- equality, identity, "inclusion" with keyword "in"; analysis of the abstractions around all of these; relevant examples of bad performance and unexpected results.
- good coverage of mutable "reference" types in Python and how they behave inside functions. This messes people up all the time; Mertz presents the problem clearly and thoroughly.
Mertz has a real passion for efficient text processing. This comes across in a subsection on regular expression pattern matching.
Like most of his writing and his books, Mertz' work here aims to get you to think about the problems presented in a certain way rather than present a code pattern for memorization or strict adherence. While this book is more practical and concise than most, it still left me thinking about more general ideas. The main thing I was left with was a heightened awareness of where code can go wrong in unexpected ways (numeric conversions gone wrong, text interpreted as a sequence, etc.) and how that relates to the implementation of programming languages (not just Python).