When you're ready to go beyond the basics, I recommend Effective Python [4] and Serious Python [5]. Both go deeper on classes, metaclasses, how objects are represented internally, and Serious Python covers a bit of functional programming, scaling, etc.
[1] https://www.fluentpython.com/
[2] http://www.dabeaz.com/python-distilled/
[3] https://automatetheboringstuff.com/
1. Explain to me like i’m 5, what exactly __slots__ is for? Write me some demo code to illustrate - the reason i’d set this challenge is because you’d come out the other side knowing how objects work and how it’s more or less dicts all the way down (to be clear, slots is an exception to the dicts all the way down view which is why i think its a good example to get stuck into)
2. Write me some demo code that explains the purpose of locals()
3. Teach me how to use dis.dis(), explain to me what each possible opcode means
4. Explain to me how to use pdb, include conveniences like breakpoint() in your explanation
5. Show me 3x working examples of calling a c function from python. Explain the different approaches you took
If you can nail these 5 questions, I’d feel pretty confident betting on you knowing enough python to be able to take on something really advanced like modifying the supported grammar of jinja for example.
https://tenthousandmeters.com/tag/cpython/
https://rushter.com/blog/tags/cpython/
https://eli.thegreenplace.net/tag/python-internals
https://realpython.com/cpython-source-code-guide/
I'm not aware of any good books about Python's internals.
I am a Deep Learning Research Engineer who works 100% in Python.
I had just started second year in college with only basic knowledge in imperative C. Didn't even know how to write a C function other than main().
Then I started learning Python with Python Crash Course by Eric Matthes.
I finished the entire book in five days. I really enjoyed it and learned a lot.
This is the ONLY programming book I read cover-to-cover, and the only Python book I have ever read.
This book served me greatly.
When anyone asks for Python book recommendation, I recommend this book only.
You don't need any other.
This book has the basics taught, then goes through three projects. One of them is a game. I remember.
All the exercises are great, self-contained, doable without outside help, and help you grow.
And the book's design has all that No Starch Press goodness to it.
Go for it.
The books just collect dust - and yet, every few years I try this experiment again with the same result.
Kinda interesting aside: I’ve been told I write great code in a particular language, while eschewing near universal patterns - patterns I’d probably have picked up if I’d gotten to Chapter 3 of any of these books. (It’s hard to independently discover cargo cult habits).
My take on the question: no book necessary.
The advantage of this is that you learn by looking at examples, and at the same time you learn the standard library. In my opinion it's much more valuable to learn the standard library of a language once you know a little bit of the language itself, because it helps you write useful programs and learn the language in that way.
https://www.youtube.com/playlist?list=PLV7iu6GiDKwQWZbapY5mP...
Like you I want to know more about CPython behind the scenes, not how to use Python itself. I like the deeper understanding
This teaches 90 specific ways to be more Pythonic
* Robust Python
This teaches some architectural / implementation patterns for code bases larger than a script
* High Performance Python
How to get the most juice out of Python, including compiling
Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to Programming, by Eric Matthes is my favourite for self study the basics.
[1] https://www.youtube.com/watch?v=cKPlPJyQrt4&ab_channel=PyDat...
It is so easy to go to SO and copy and paste code and not know what is really going on. I think understanding Python at a deeper level will be even more important now that chatGPT will spit out the code for you and happily lie about some things.
All three are quite well written, kept more reliably up to date than any other resource, and provide links to a wealth of other material.
A particularly useful point is the link to the source code found at the top of most of the Standard Library pages… Python’s source code is really literate, and often the best way of understanding what’s happening “under the hood” is to actually look, rather than rely on someone else’s summary or intuition of what maybe only _was_ under the hood.
[0] https://www.amazon.com/Learn-Python-Hard-Way-Introduction/dp...
For learning python (if you already know some programming) I always recommend “Introudcing Python” by Lubanovic it gives a coverage of all the major python features.
Once you are able to code basic python I use the book “Fluent Python” by Ramalho (I still use it to this day even as an advanced python programmer). This book has an amazing coverage of every single advanced feature in python. Anytime you’re on the job and you know you could use some fancy classes or some async stuff or maybe multi threading you can visit a chapter in this book and get up to speed on it. I probably open this book once a month just to refresh my knowledge on some particular python feature I want to use.
I also recommend reading the official Python documentation, especially when working on things, and looking at functions within whatever module you are using that you are not using. Over time you will discover functions / methods you had no idea were in there. I also suggest just exploring other modules you have yet to use.
I would recommend compiling a debug build of Python and stepping through it while it executes your script, even if it takes you an entire day for a simple script. Python is a big dictionary sandwich. Everything is a dictionary, even dictionaries.
What does
A.b.c() do?
You do not need to know the Python internals. The semantics are good enough, but the leak through everywhere. You might enjoy using https://micropython.org/ to learn instead.
Its lessons are free, open source, and extremely accessible. Nice and short, too.
And, once you're proficient, you can contribute back to it!
It's not a book, but I loved it and it is really helpful.