tag «CPython»

  1. 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 name a, 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?
    Today we'll answer these questions and understand how variables, so crucial aspect of a programming language, are implemented in CPython.

    read more
  2. 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 main() 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.

    read more
  3. Python 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 block's bytecode, constants and names of variables used in the block and block's various properties.

    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.

    read more