For many programmers, the hardest part isn’t the first few steps. It’s the leap from knowing the basics to writing elegant, expressive code. Python programmers often find this hill as difficult to climb as learners of more complex languages.
Rob Sweigart, author of the excellent , aims to do something about that with his new book, . The earlier book was aimed at absolute beginners who wanted to get started with Python and do practical, useful things out of the gate. The new book is in some respects a successor to Automate, but as Sweigart notes in his introduction, Beyond the Basic Stuff can follow up most any introductory book to Python.
Sweigart focuses on three major subjects: common difficulties in getting started (seeking help, setting up a work environment); best practices, tools, and techniques; and using object-oriented Python. The second section is the largest in the book and somewhat sprawling—perhaps the parts on tools, like black and git, could have been spun off separately—but each section can be read on its own.
The book is all the more useful for collecting together between one pair of covers material that you would typically dig up from multiple resources. Many of the chapters deal with code style—not just formatting with tools like black, but dealing with code “smells” (bad habits that often spawn bugs), making code more “Pythonic” (using Python’s native idioms well), and avoiding some of the gotchas afflicting even seasoned developers.
Sweigart takes time to explain the goals or motivation behind specific advice. With gotchas, for instance, there’s a detailed treatment of a common Python pitfall: how to deal with changing the contents of a list you’re looping over. Sweigart doesn’t just say, “Don’t change an existing list, create a new list and append to it.” Rather, he walks you through the details of why modifying a list as you loop through it can have unpredictable results. He even takes the time to dispel worries about why the suggested solution is not as inefficient as it might seem.
Many of the chapters serve as good jumping-off points for more involved study. However, that doesn’t mean they’re skimpy, just concise. An entire book could be written about Python performance optimizations—in fact, —but Sweigart touches on some of the most important aspects of that in one chapter. He takes the time to explain the concept of algorithmic time complexity, or , in plain language, and walks the reader through using cProfile to garner live performance statistics about their apps.
Some of the most difficult concepts for a newly minted Python programmer to grok are object-oriented programming (OOP) and classes. Sweigart devotes an entire subsection of the book, three chapters, to explaining how OOP and classes work in Python. Sweigart uses a slew of sensible real-world examples to do this, from the usual “class = car blueprint, object instance = car” example to more nuanced ones. Most importantly, he connects the behaviors and components of classes with Python concepts beginners are already familiar with, useful context not always provided in these discussions.
There are other books for Pythonistas who have graduated from basic training and want to move to the next level. Fluent Python, for instance, is an overall guide to making code more “Pythonic,” as the Python masters put it. Beyond the Basic Stuff with Python aims more broadly than just code.
Copyright © 2021 IDG Communications, Inc.