Python & FastAPI•Part 1: Candid Experience, Architecture & Intro (1 - 5)
What is Python, and what are its core architectural characteristics (interpreted, dynamically typed, multi-paradigm)?
In Simple Words: Python is a high-level, general-purpose, interpreted, dynamically typed, garbage-collected programming language created by Guido van Rossum in 1991. It emphasizes readability, rapid developer productivity, and clean syntax ("batteries included").
Core Architectural Characteristics:
- Bytecode Compilation & PVM Execution: Python source code (
.py) is first compiled into bytecode (.pyc), which is then executed by the CPython Virtual Machine (PVM). - Dynamic & Strong Typing: Variables are dynamic (they can point to values of any type at runtime), but the language is strongly typed (Python will not silently coerce types, e.g.
"5" + 2throws aTypeError). - Multi-Paradigm: First-class support for Object-Oriented Programming (classes, multiple inheritance), Functional Programming (map, filter, list comprehensions, lambdas), and Procedural code.
- Memory Management: Automatic memory management combining Reference Counting (immediate deallocation when references hit 0) with a Generational Garbage Collector (detecting reference cycles in Generations 0, 1, and 2).
- The Global Interpreter Lock (GIL): A mutex that protects CPython object memory, preventing multiple native threads from executing Python bytecode simultaneously within a single process. Concurrency is achieved via AsyncIO (for I/O-bound tasks) or multiprocessing (for CPU-bound tasks).
python
# Dynamic introspection and bytecode disassembly in Python:
import dis
import sys
# 1. Dynamic typing with strong type enforcement
x = 42
print(f"Value: {x}, Type: {type(x)}, RefCount: {sys.getrefcount(x)}")
try:
result = "100" + x # Strong typing prevents implicit string/int addition
except TypeError as e:
print(f"Strong typing in action: {e}")
# 2. Inspecting Python bytecode with dis:
def calculate_square(num: int) -> int:
return num * num
print("\nCPython Bytecode Disassembly:")
dis.dis(calculate_square)