Understanding How Python Works Internally

How Does Python Work Behind the Scenes?

Internal-working-of-Python-(1)

1. Source Code Execution

  • When you write Python code, this source code isn't directly understood by your computer.

  •   # my_program.py
      def greet(name):
          return f"Hello, {name}!"
    
      message = greet("Pythonista")
      print(message)
    

2. Compilation to Bytecode

  • Python first compiles your code into an intermediate format called bytecode.

    • This is not the same as machine code. It’s a lower-level, platform-independent code.

    • The .py files you write often get converted to .pyc files (compiled Python code).

    • These .pyc files are stored in a folder named __pycache__.

3. Python Virtual Machine (PVM)

  • The generated bytecode is then sent to the Python Virtual Machine (PVM).

    • PVM reads and executes the bytecode line by line.

    • The PVM acts like a runtime engine, similar to how a car needs an engine to run.

    • This is why Python is called an "interpreted" language, even though there is a compilation step to bytecode.

4. Is It Compiled or Interpreted?

  • Python is neither purely compiled nor purely interpreted.

    • It compiles code to bytecode (intermediate step).

    • Then the PVM interprets and executes the bytecode.

5. What Is Bytecode?

  • Bytecode is not machine code – your computer’s hardware cannot run it directly.

  • It is a set of instructions that the PVM understands, making it portable across different platforms (Windows, Mac, Linux).

6. Why Bytecode?

  • Bytecode allows Python code to run fast (faster than interpreting source code every time).

  • Recompilation is needed only if you change your code, otherwise the cached bytecode is reused.

7. Different Python Implementations

  • CPython is the most common implementation and what most people use.

  • There are other implementations as well: Jython (for Java), IronPython (for .NET), PyPy (with performance optimizations), etc.

8. Special Folders and Files

  • The __pycache__ folder stores these cached bytecode files.

  • These help in speeding up your program’s startup time and execution.

9. Interview Insights

  • Bytecode is not the same as machine code.

  • Bytecode is platform-independent; machine code is platform-specific.

  • Understanding inner workings helps in writing efficient Python code and is useful in technical interviews.

In short:
When you run Python code, it’s first compiled to bytecode (which is not the same as the code your hardware understands). Then, the Python Virtual Machine (PVM) executes this bytecode. Bytecode and the PVM together provide Python’s famous cross-platform flexibility and make your code work on any system with Python installed.

0
Subscribe to my newsletter

Read articles from Sparsh Jaipuriyar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sparsh Jaipuriyar
Sparsh Jaipuriyar