Python inner working

How Python Works Internally (Inner Workings of Python)

Python is an interpreted, dynamically-typed, and high-level language. Let’s break down its inner workings step by step.


1. Python Code Execution Process

When you write and run a Python script (.py file), Python internally goes through several steps:

1️⃣ Source Code (.py file)

  • You write Python code in a .py file.

  • Example:

      pythonCopyEditprint("Hello, World!")
    

2️⃣ Lexical Analysis (Tokenizer)

  • Python breaks the code into tokens (small meaningful units).

  • Example: print("Hello, World!") is broken into:

      csharpCopyEdit[PRINT, STRING_LITERAL("Hello, World!"), NEWLINE]
    

3️⃣ Parsing (Syntax Analysis)

  • Python checks the syntax using the tokens and creates a parse tree.

  • If there are syntax errors, Python throws an error.

4️⃣ Abstract Syntax Tree (AST)

  • The parse tree is converted into an AST (simplified tree structure).

  • Example:

      scssCopyEditAST of print("Hello"):
        ├── Print
        ├── String("Hello")
    

5️⃣ Bytecode Generation (.pyc file)

  • The AST is converted into Python Bytecode (.pyc), which is a low-level, platform-independent set of instructions.

  • Example of bytecode:

      bashCopyEditLOAD_GLOBAL 0 (print)
      LOAD_CONST 1 ('Hello, World!')
      CALL_FUNCTION 1
      POP_TOP
    

6️⃣ Python Virtual Machine (PVM)

  • The bytecode is executed by the Python Virtual Machine (PVM).

  • The PVM is responsible for running your Python program.


2. Python Memory Management

Python manages memory automatically using: ✅ Reference Counting
Garbage Collection (GC)
Dynamic Memory Allocation

(a) Reference Counting

Python keeps track of how many times an object is referenced.

pythonCopyEditx = "Hello"  # Reference count = 1
y = x        # Reference count = 2
del x        # Reference count = 1 (but object still exists)

If reference count reaches zero, Python deletes the object.

(b) Garbage Collection (GC)

  • Python has an automatic garbage collector that removes unused objects.

  • Uses Generational Garbage Collection to manage memory efficiently.

(c) Memory Allocation (Heap & Stack)

  • Stack Memory: Stores function calls and local variables.

  • Heap Memory: Stores objects like lists, dictionaries, and user-defined objects.


3. Python Internals: Objects & Variables

In Python, everything is an object (numbers, strings, functions, etc.).

pythonCopyEditx = 10
print(id(x))  # Returns memory address of x
  • Variables in Python are references (pointers) to objects in memory.

4. Python's Global Interpreter Lock (GIL)

Python uses the GIL, which means:

  • Only one thread can execute Python bytecode at a time.

  • This can limit multi-threading performance.

  • Solution? Use multiprocessing instead of threading for CPU-intensive tasks.


5. Python's Built-in Data Structures

Python provides efficient built-in data structures:

  • Lists ([]) → Dynamic arrays

  • Tuples (()) → Immutable sequences

  • Dictionaries ({}) → Hash tables (key-value pairs)

  • Sets ({}) → Unordered collections of unique elements


6. Python's Interpreter

Python has different implementations:

  • CPython (Default, written in C)

  • PyPy (Uses JIT for faster execution)

  • Jython (Runs on Java Virtual Machine)

  • IronPython (Runs on .NET framework)


Summary: Python's Inner Workings

1️⃣ Python converts source code → tokens → AST → bytecode
2️⃣ Bytecode runs on Python Virtual Machine (PVM)
3️⃣ Memory management uses reference counting & garbage collection
4️⃣ GIL restricts threading, so multiprocessing is preferred
5️⃣ Everything in Python is an object