Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 14 / Topic 1 of 11

Everything is an object

In Python every value is an object: numbers, strings, lists, functions, classes, even modules. Every object has three things:

Part Question it answers How to see it Can it change?
identity which object is this? id(obj), is never
type what kind of object? type(obj) never (in practice)
value what data does it hold? print(obj), == only for mutable objects

Think of a parcel at a courier office: the tracking number (identity), the parcel type, say "documents" (type), and what is inside (value).

import sys

print(type(1760).__name__)       # int
print(type(len).__name__)        # builtin_function_or_method
print(type(sys).__name__)        # module
print(type(int).__name__)        # type   (a class is an object too)

Names vs objects, in depth

You met this in Part 2. A name (variable) is only a label stuck on an object. = never copies an object; it sticks another label on it.

cart = ["rice", "dal"]
same_cart = cart                 # a second label on the SAME list
copy_cart = ["rice", "dal"]      # a NEW list with an equal value

print(cart == copy_cart)         # True   (same value)
print(cart is copy_cart)         # False  (different objects)
print(cart is same_cart)         # True   (one object, two names)

same_cart.append("ghee")
print(cart)                      # ['rice', 'dal', 'ghee']
  • == asks "equal value?"; is asks "the very same object?".
  • Use is only for None (if result is None:), never for numbers or strings.
  • Passing an object to a function passes the same object, so a function can change a list you give it (Part 5).

Reference counting

A reference is anything that points at an object: a name, a slot in a list, a dict value. CPython keeps a reference count in every object: how many references point at it. When the count drops to 0, the object is freed at once. Think of a shared office desk: it is cleared as soon as the last person who uses it leaves.

sys.getrefcount(obj) shows the count. It is always one more than you expect, because passing obj to getrefcount makes one extra, temporary reference (the function's own argument).

import sys

order = {"id": 101, "total": 1760}
print(sys.getrefcount(order))    # 2   (the name `order` + the temporary argument)

backup = order
print(sys.getrefcount(order))    # 3   (+1 for `backup`)

orders = [order]
print(sys.getrefcount(order))    # 4   (+1 for the list slot)

del backup
orders.clear()
print(sys.getrefcount(order))    # 2   (back to the start)

del name removes the name, not the object. The object is freed only when its count reaches 0.

๐Ÿ” Going deeper: since CPython 3.12, some objects are immortal (never freed): None, True, small integers. sys.getrefcount(None) shows a huge fixed number (3221225472 on CPython 3.14, 64-bit). Compare changes in the count, not exact numbers.

Full program: examples/01_objects_and_refcounts.py.

Done here? The practice set for Part 14 โ€” 8 exercises with solutions โ€” is on the part page.

Search the guide