Move Beyond Python Code That "Mostly Works" to Code That Is Expressive, Robust, and Efficient
Python is arguably the most-used programming language in the world, with applications from primary school education to workaday web development, to the most advanced scientific research institutes. While there are many ways to perform a task in Python, some are wrong, inelegant, or inefficient. Better Python Code is a guide to "Pythonic" programming, a collection of best practices, ways of working, and nuances that are easy to miss, especially when ingrained habits are borrowed from other programming languages.
Author David Mertz presents concrete and concise examples of various misunderstandings, pitfalls, and bad habits in action. He explains why some practices are better than others, based on his 25+ years of experience as an acclaimed contributor to the Python community. Each chapter thoroughly covers related clusters of concepts, with chapters sequenced in ascending order of sophistication.
Whether you are starting out with Python or are an experienced developer pushing through the limitations of your Python code, this book is for all who aspire to be more Pythonic when writing better Python code.
Use the right kind of loops in Python Learn the ins and outs of mutable and immutable objects Get expert advice to avoid Python "gotchas" Examine advanced Python topics Navigate the "attractive nuisances" that exist in Python Learn the most useful data structures in Python and how to avoid misusing them Avoid security mistakes Understand the basics of numeric computation, including floating point numbers and numeric datatypes "My high expectations for this engaging Python book have been it offers a great deal of insight for intermediate or advanced programmers to improve their Python skills, includes copious sharing of precious experience practicing and teaching the language, yet remains concise, easy to read, and conversational." --From the Foreword by Alex Martelli
Register your book for convenient access to downloads, updates, and/or corrections as they become available. See inside book for details.
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).