I have been paid money to write Python code since about 2006, so I figured it was probably time that I should understand some of the inner workings of Python. I therefore picked up two books on the topic, this one being the first of the two.
This book to be honest isn’t completely what I expected. Its very well written and quite interesting, but its more about the things you’d need to know to become a Python core developer, rather than the things you should know as a user of Python like how the Python dictionary implementation is built.
(If you want that specifically, this video is an excellent introduction).
The book starts with the assumption that you are going to want to customize Python and recompile it (which while educational is probably a truly terrible idea in the real world), so the first couple of chapters are devoted to setting up a development and compilation environment for Python on various platforms, which seems fair enough to me. The book then moves into a discussion of how the language grammar is defined and parsed, which is something I haven’t really thought about since my compilers course at university.
Interestingly, within the first couple of chapters of the book I had learnt about Python syntax constructs I was unaware of as someone who struggles a bit to keep you with the development of the language over time. So even that aspect has been quite useful. I’ll document some examples of new syntax I learnt in another blog post so as to keep this post about the book itself.
The book then moves on to describing at a high level how the Python bytecode runtime works. It then describes memory allocation. Next is multiprocessing including the exact mechanism that Python uses to fork a new process or thread — punch line, its more complicated than it would look if your mental model is how fork works in C. These topics are all dealt with in detail, which is likely helpful to total computer science newbies, but is sometimes a bit excruciating if you already know what a semaphore is.
The coverage of misuses of yield, coroutines, and async is quite good I think. Sub interpreters sound interesting too, although too experimental for use quite yet. There is extensive coverage of the various Python data types, including how the Python number type is slightly bonkers and how unicode works under the hood. There is in fact even some coverage of how dictionaries work, but the video above is better to be honest.
Overall I enjoyed this book and I definitely learned things. I wouldn’t recommend it to a Python newbie, but if you’re an experienced developer and want to understand of what life is like under the hood then this is a good place to start.
May 5, 2021
396
Get your guided tour through the Python 3.9 interpreter: Unlock the inner workings of the Python language, compile the Python interpreter from source code, and participate in the development of CPython. Are there certain parts of Python that just seem like magic? This book explains the concepts, ideas, and technicalities of the Python interpreter in an approachable and hands-on fashion. Once you see how Python works at the interpreter level, you can optimize your applications and fully leverage the power of Python.