Objects
An object is a concrete value in memory: it has an identity, a type, and data. Numbers, strings, lists, functions, and class instances are all objects. There are no unboxed primitives in Python.
Detailed Explanation
When people say "object" they sometimes mean "instance of a class I wrote". In Python the word is broader. 12, "ada", [1, 2], len, and Order("A-1", 2) are objects. Each has an identity (id(obj)), a type (type(obj)), and a value. The identity is stable for the object's lifetime. The type decides which operations exist. The value is the payload: the integer, the characters, the attributes.
Instances of user classes hold attributes in a __dict__ by default. self.sku = "A-1" writes that dict. You can also use __slots__ to fix the set of names and skip the dict. Lookup walks the instance, then the class, then the bases along the MRO. Assignment to self.name always writes the instance (unless a data descriptor intercepts it).
Objects are referenced, not copied, when you assign or pass them. alias = order does not clone order. Mutating alias.qty changes order.qty. That is ordinary Python semantics and the reason mutable default arguments and shared class lists surprise people.
Everything being an object has a cost. A small int is a heap allocation with a header, not a machine word the CPU adds directly. CPython mitigates this with interned small integers and with C extensions that operate on arrays of raw numbers (NumPy). For application code the object model is the point: uniform semantics, runtime introspection, and duck typing.
object is the root type. A class with no bases inherits from it. object() is a blank instance you can use as a sentinel: MISSING = object(). Two such sentinels compare unequal by identity, which is what you want.
Equality and identity are different questions. a is b is identity. a == b is a.__eq__(b). Two objects can be equal and not be the same object. Use is for None and sentinels. Use == for values.
Code Examples
order = {"sku": "A-1", "qty": 2}
alias = order
alias["qty"] = 3
print(order["qty"]) # 3, same object
print(id(order) == id(alias)) # True
print(order == {"sku": "A-1", "qty": 3}) # True, equal value
print(order is {"sku": "A-1", "qty": 3}) # Falseclass Order:
def __init__(self, sku: str):
self.sku = sku
line = Order("A-1")
print(type(line)) # <class '__main__.Order'>
print(line.__dict__) # {'sku': 'A-1'}
print(isinstance(line, object)) # True
print(isinstance(12, object)) # TrueMISSING = object()
def price_of(row: dict) -> object:
return row.get("price", MISSING)
print(price_of({}) is MISSING) # True
print(price_of({"price": 0}) is MISSING) # FalseCommon Gotchas
Assignment does not copy
`b = a` aliases. Use `copy.copy` / `copy.deepcopy`, `list(a)`, or `a.copy()` when you need a second object.
`id()` is not a serial number you should persist
CPython may reuse an id after the object dies. Compare live objects with `is`, do not store `id`s.
Two equal values are not always interned
`is` on strings or ints can look true in tests and false in production. Use `==`.
Functions, classes, and modules are objects
they have attributes you can set. That is powerful and a good way to create hidden global state.