What happens when we get or set an attribute of a Python object? This question is not as simple as it may seem at first. It is true that any experienced Python programmer has a good intuitive understanding of how attributes work, and the documentation helps a lot to strengthen the understanding. Yet, when a really non-trivial question regarding attributes comes up, the intuition fails and the documentation can no longer help. To gain a deep understanding and be able to answer such questions, one has to study how attributes are implemented. That's what we're going to do today.
read morePython behind the scenes #6: how Python object system works
As we know from the previous parts of this series, the execution of a Python program consists of two major steps:
read more
1. The CPython compiler translates Python code to bytecode.
2. The CPython VM executes the bytecode.
We've been focusing on the second step for quite a while. In part 4 we've looked at the evaluation loop, a place where Python bytecode gets executed. And in part 5 we've studied how the VM executes the instructions that are used to implement variables. What we haven't covered yet is how the VM actually computes something. We postponed this question because to answer it, we first need to understand how the most fundamental part of the language works. Today, we'll study the Python object system.Python behind the scenes #5: how variables are implemented in CPython
Consider a simple assignment statement in Python:
a = b
The meaning of this statement may seem trivial. What we do here is take the value of the name
b
and assign it to the namea
, but do we really? This is an ambiguous explanation that gives rise to a lot of questions:- What does it mean for a name to be associated with a value? What is a value?
- What does CPython do to assign a value to a name? To get the value?
- Are all variables implemented in the same way?
Python behind the scenes #4: how Python bytecode is executed
We started this series with an overview of the CPython VM. We learned that to run a Python program, CPython first compiles it to bytecode, and we studied how the compiler works in part two. Last time we stepped through the CPython source code starting with the
read moremain()
function until we reached the evaluation loop, a place where Python bytecode gets executed. The main reason why we spent time studying these things was to prepare for the discussion that we start today. The goal of this discussion is to understand how CPython does what we tell it to do, that is, how it executes the bytecode to which the code we write compiles.Python behind the scenes #3: stepping through the CPython source code
In the first and the second parts of this series we explored the ideas behind the execution and the compilation of a Python program. We'll continue to focus on ideas in the next parts but this time we'll make an exception and look at the actual code that brings those ideas to life.
read morePython behind the scenes #2: how the CPython compiler works
In the first post of the series we've looked at the CPython VM. We've learned that it works by executing a series of instructions called bytecode. We've also seen that Python bytecode is not sufficient to fully describe what a piece of code does. That's why there exists a notion of a code object. To execute a code block such as a module or a function means to execute a corresponding code object. A code object contains the block's bytecode, the constants and the names of variables used within the block and the block's various properties.
read more
Typically, a Python programmer doesn't write bytecode and doesn't create the code objects but writes a normal Python code. So CPython must be able to create a code object from a source code. This job is done by the CPython compiler. In this part we'll explore how it works.Python behind the scenes #1: how the CPython VM works
Have you ever wondered what
python
does when you run one of your programs?$ python script.py
This article opens a series which seeks to answer this very question.
read more