I’m looking to learn about this language from a technical level, and any searches I do on today’s search engines is just going to return guides to writing Python code, which is not what I want.
I understand how C++ works. For example, I know that virtual functions are stored as a trap table in an object’s instance, and the function is wrapped around something that decodes that trap table from this
object instance.
I’m wondering if there’s something that goes into that level of technicality with python. For example, I would want to know how function declarations (and re-declarations) work in python. Is the bytecode stored as a heap object which can be freed just as a regular heap object? Is it a map of strings within the current stack context? How does creating a thread handle it?
If you have not already read through it, there is a ton of useful information about Python’s data model in the user manual; it is my go-to resource when I want to do weird stuff with metaclasses and the like.
Furthermore, you might find it interesting to peruse the C code that is generated by Cython, because it gives you a concrete view of the kinds of steps that Python has to go through from a C perspective to work within its data model. (Cython is a bit faster than Python because it does not have to interpret bytecode, but unless you use special directives it still has to e.g. do general attribute lookups whenever you interact with a Python value, even if the value is an integer, and maintain reference counts.)
Finally, you might also get a lot out of skimming through the CPython bytecode instructions, as this has a lot of interesting details about how the bytecode interpreter works in practice.
search for w3schools
I guess when you want to know how it works under the hood, this article may give you a good overview. “under the hood” is my go to keyword when I want to know more about the internals.
https://www.codewithc.com/how-python-works-under-the-hood-pythons-internal-mechanics
There’s the Cpython Internals book, as well as Brett Cannon’s Syntatic Sugar which I think might help with some of your questions (though admittedly, I’m just interested in data sciencey stuff)
ChatGPT literally gave me about 5 pages of answer to your question with sources. Here is a high level excerpt, but it also included detailed tables and lots of additional resources. Give it a go, it’s a much better search engine than Google which is just an ad serving engine now.
From GPT 4o:
You’re looking for an in-depth, systems-level understanding of Python—how its runtime, memory model, bytecode, scoping, and threading actually work under the hood. That’s great, and here are some of the best resources and directions for diving in:
⸻
🔬 Core Resources for Python Internals
-
The Python Source Code (CPython) • GitHub: https://github.com/python/cpython • Best way to understand function declarations, object lifecycle, bytecode, and threading behavior. • Start in: • Objects/funcobject.c – how function objects are defined • Python/compile.c and Python/ast.c – compilation to bytecode • Include/object.h – memory layout of Python objects
-
Python Bytecode Disassembly • dis module in Python: import dis; dis.dis(some_function) • Shows you stack-machine bytecode operations used by the Python interpreter. • Combine with CPython source to see how bytecode maps to execution.
-
“Deep Dive” Blogs & Books • “Python Internals” by Phillip J. Eby Very readable, walks through LOAD_GLOBAL, function calls, closures, etc. • “Fluent Python” by Luciano Ramalho – readable but digs into things like descriptors, metaclasses, dunder methods, etc. • “Inside the Python Virtual Machine” by Obi Ike-Nwosu Probably the closest C+±like systems-level explainer you’ll find. Covers: • how the evaluation stack works • memory layout of objects • function instantiation and call frames
An LLM doesn’t do search. It regurgitates the statistically most likely next words based on its training data.
Not true. Some LLM implementations can parse your prompt, choose search terms from it, search, read results, and parse results into a response.
https://www.technologyreview.com/2024/10/31/1106472/chatgpt-now-lets-you-search-the-internet/
-
Do you mean function definitions? They are executable statements after all. Yes the Python environment is just a bunch of nested dictionaries. Whether there is bytecode is up to the implementation. If you want to understand how CPython works, the source code is not terribly mysterious if you know C. You will want to read the API document first.
For the language itself, the reference manuals are reasonably good.