Search⌘ K
AI Features

Everything Is an Object

Explore how Python treats everything as an object including variables, functions, and modules. Understand object identity, type, and value, and how variables reference objects rather than storing values. This lesson helps you grasp the concept of shared references, aliasing, and the difference between identity and equality, laying a foundation for understanding Python's memory model and mutability.

To write effective Python code, it helps to understand Python’s execution model. In many languages, a variable is often described as a container that stores a value. Python works differently.

In Python, values exist as objects in memory, and variables are simply names that reference those objects. Assigning a variable does not copy or store a value; instead, it binds a name to an existing object, much like attaching a label to a location in memory. While this distinction may appear subtle at first, it is fundamental to understanding how data moves through a Python program.

This lesson recaps some key concepts studied earlier to help you understand how these ideas are applied in more advanced contexts.

The Python memory model: Variables as references

For an assignment like x = 100, Python performs two distinct steps:

  1. Creates an integer object representing the value 100 and stores it at a specific location in memory.

  2. Then, it creates the name x in the current namespace and binds it to that object.

In this context, x is said to be a reference to the integer object 100. The variable does not store the value itself; instead, it identifies where the object resides in memory. If x is later reassigned to 200, Python does not modify the original object. Rather, it creates a new integer object representing 200 and rebinds x to reference this new ...