Book Review: Effective Python: 59 Specific Ways to Write Better Python

Lately, I’ve been reading Effective Python: 59 Specific Ways to Write Better Python. When I randomly selected this book, mostly based on its title, I had already beenEffective Python: 59 Specific Ways to Write Better Python working on an established Python project at work for a few months. Therefore I already had a bit of experience on good practices, do’s and don’ts. But coming from languages like C, C++, and Java, I had a bit of issue with the whole Pythonic Way of things.

The first chapter of this book talks about the language and the Pythonic Way of doing things. Since I had been working a few months on a Python project, this chapter was not that interesting to me. But I would have loved this kind of information when I first started working in Python. It covers small things like PEP8 style, slicing, list comprehension, generators, and various other language-level things.

The second chapter revolves around functions. A few basic concepts are presented, for example: how to document a function and how to write a generator. But a few more interesting are also covered: how closures interact with variable scopes, star arguments, and keyword arguments. But what was most interesting to me were all the details about the differences between Python 2 and Python 3 for all of these.

The third chapter covers the basis around classes and inheritance. To me, the one interesting part of this chapter was the examples using ABCs (Abstract Base Classes). It got me interested in the concept of metaclass in Python.

Luckily for me, chapter four covered metaclasses. For the first time since starting this book, I applied something I read from it at work: we expose plugins and callbacks to other teams and had no way of ensuring that they implement all these correctly; Now, using metaclasses we can ensure that they at least have the correct methods in their plugins. It’s not much, but it’s still a lot better than it used to be.

Chapter 5 could have been summarized in one sentence: never use threads for CPU-bound computation, only for I/O; use sub-processes for CPU-bound computation. This is something I had already learned while working at my job. One of my first tasks on my new team was to optimize one of our services. We basically moved a bunch of processing from background threads to the main thread, keeping the background threads for I/O only. This gained us a lot of processing power. Next thing we did was to reduce the number of threads in our process, we noticed that this also helped a lot the performance of the service. We moved these to a second process that now handles only I/O threads, lots of these, while the main process generates things to do.

Chapters 6 and 7 go about explaining a little about libraries and tools. Amongst other things, it explains about virtual environments, pip, functools and other little things like this. Again, I did not really learn anything here, to me these are needed on any Python project, and I was already using them.

The last chapter gives a few pointers on how to setup your application to have different behavior in production, development, and testing; reminds you to always test your application and shows a few tools to help you debug/optimize once you start having issues. In this chapter, I found something that looked super interesting: tracemalloc. Then I saw that it exists only in Python 3, and the project I’m working on is still using Python 2 🙁

All things considered, I’d recommend Effective Python: 59 Specific Ways to Write Better Python, but mostly for people that are starting in Python, or that are working on projects that do not use pip/virtual env/etc.

Do you have any advanced Python books you recommend? I’m looking into advanced topics, around the language and tools.

Thanks for reading !