Skip to main content

1. Memory Model

Python manages memory automatically, but understanding the basics helps write efficient code.


Key Concepts

  • Objects: Everything in Python is an object stored in memory.
  • References: Variables are references (pointers) to objects.
  • Heap: Objects are stored in a private heap managed by the Python interpreter.

Example

x = [1, 2, 3]
y = x
y.append(4)
print(x) # [1, 2, 3, 4]

Both x and y point to the same object in memory.


Wrap-Up

Understanding Python's memory model is key for avoiding unintended aliasing and writing efficient code.