on
interpreter. In CPython, its reference implementation, this is a program
written in C that loops through the bytecode given to it, and interprets the
instructions in it one by one.

The heart of this machinery is implemented in [ Python/ceval.c
](https://github.com/python/cpython/blob/v3.7.0b1/Python/ceval.c) file. It
runs an infinite loop that contains a [ giant switch statement
](https://github.com/python/cpython/blob/v3.7.0b1/Python/ceval.c#L1056) with
each case (target) handling on of the possible bytecode operations.

This is how [ the code
](https://github.com/python/cpython/blob/v3.7.0b1/Python/ceval.c#L1264-L1288)
for the instruction BINARY_ADD looks like:

    
    
    TARGET(BINARY_ADD) {
        PyObject *right = POP();
        PyObject *left = TOP();
        PyObject *sum;
    
        /* the computation of the "sum" omitted */
    
        SET_TOP(sum);
        if (sum == NULL)
            goto error;
        DISPATCH();
    }
    

` POP() ` , ` TOP() ` , and ` SET_TOP() ` are convenience C macros that
perform primitive interpreter stack operations such as popping the top value
from the stack, or replacing the current TOS (Top Of Stack) value with a
different one.

The code above is actually pretty straightforward. It pops the right-hand
operand from the top of the stack, which is a reference to ` y ` in our case,
and stores it under the name ` right ` . It then also stores a reference to
the left-hand side operand, i.e. ` x ` , that became the new TOS.

After performing the calculation, it sets the ` sum ` , i.e. a reference to
the result, as the new TOS, performs a quick error check, and dispatches the
control to the next bytecode instruction in the line.

In [ Part 2 ](https://blog.peterlamut.com/2018/02/26/how-python-
computes-2-5-under-the-hood-part-2/) the representation of Python objects at C
level is explained, and how _adding_ two such objects is done.

###  Share this:

  * [ Twitter  ](https://blog.peterlamut.com/2018/02/04/how-python-computes-2-5-under-the-hood-part-1/?share=twitter "Click to share on Twitter")
  * [ Facebook  ](https://blog.peterlamut.com/2018/02/04/how-python-computes-2-5-under-the-hood-part-1/?share=facebook "Click to share on Facebook")
  * 

Like  Loading...

![](https://1.gravatar.com/avatar/47889a61e55c396e2ea18b0de05fa8a63f36cf32eddcf552613be0ebca37fb83?s=49&d=identicon&r=G)
Author  [ Peter Lamut ](https://blog.peterlamut.com/author/peterlamut/) Posted
on  [ February 4, 2018  February 26, 2018
](https://blog.peterlamut.com/2018/02/04/how-python-computes-2-5-under-the-
hood-part-1/) Tags  [ CPython internals
](https://blog.peterlamut.com/tag/cpython-internals/) , [ Python
](https://blog.peterlamut.com/tag/python/) [ Leave a comment  on How Python
computes 2 + 5 under the hood (part 1)
](https://blog.peterlamut.com/2018/02/04/how-python-computes-2-5-under-the-
hood-part-1/#respond)

##  About the Author

![](https://1.gravatar.com/avatar/47889a61e55c396e2ea18b0de05fa8a63f36cf32eddcf552613be0ebca37fb83?s=96&d=identicon&r=G)  
I am an experienced software engineer, primarily using Python and JavaScript
in my day-to-day work, putting a strong emphasis on writing clean and
maintainable code.

I use Linux, like to crack jokes, and enjoy attending developer meetups and
conferences like EuroPython.

I do not own any pets or hair care products.

Search for:  Search

##  Tags

  * [ Attribute lookup ](https://blog.peterlamut.com/tag/attribute-lookup/)
  * [ ChainMap ](https://blog.peterlamut.com/tag/chainmap/)
  * [ Classmethod ](https://blog.peterlamut.com/tag/classmethod/)
  * [ CPython internals ](https://blog.peterlamut.com/tag/cpython-internals/)
  * [ Debugging ](https://blog.peterlamut.com/tag/debugging/)
  * [ Decorators ](https://blog.peterlamut.com/tag/decorators/)
  * [ Descriptors ](https://blog.peterlamut.com/tag/descriptors/)
  * [ Dictionary ](https://blog.peterlamut.com/tag/dictionary/)
  * [ Enum ](https://blog.peterlamut.com/tag/enum/)
  * [ Function arguments ](htt