2

I am currently mainly developing things in Python, I code in Python a lot, and I am slowly starting to wonder: "what really happens on the lower-level of Python?", I know that Python's code gets converted to binary code which then gets processed, but where can I really find out whats happening when I, for example, create a variable with a value, how does one value get represented, where is it stored, how is it stored? How does the code get converted? How do booleans work, how can I modify bytes? etc. When telling a computer what to do it's like explaining your comments to the computer, but how does the computer process all of it?

Where/how can I find out how Python or in general my computer really works (looks under the hood)? I want to learn more about what's really happening in hopes of me becoming a better programmer.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Lotus
  • 119
  • 2
  • 8
  • "I know that Python's code gets converted to binary code which then gets processed" : That's highly dependant on the implementation of Python. In CPython, it's translated to bytecode (but not real machine code), then that bytecode is run in a Python interpreter. For [CPython](https://github.com/python/cpython), reference the source. For another implementation, reference their source/docs. – Carcigenicate Mar 16 '21 at 16:54
  • For the actual interpretation in CPython, you can start [here](https://github.com/python/cpython/blob/6086ae7fd4aeb4089282189673f9bd0cc33abf9b/Python/ceval.c#L1853); although it's *quite* a dense read. Also, use the `dis` module to see the produced bytecode. – Carcigenicate Mar 16 '21 at 16:56

2 Answers2

0

If you want to look at a bytecode interpreter written in Python there is x-python. There is even a gdb (or pdb)-like debugger for this which allows you to step either Python statements or bytcode instructions.

It supports Bytecode back to around Python 2.4, but the interpreter is most complete around 3.5 opcodes and earlier.

This can be viewed as a feature for those who want to use this project to increase understanding of bytecode to the point of being able to fill in the gaps :-)

It is based on Ned Batchelder's byterun.

Note: neither project fully isolates the interpreter environment from the bytecode that is getting interpreted. But for tutorial purposes, this shouldn't be a problem.

rocky
  • 7,226
  • 3
  • 33
  • 74