Skip to main content

Python Objects

OOP Core

Working with Objects

Every class instantiates objects. Understand identity, equality, and lifecycle events for predictable behavior.

Lifecycle

  1. __new__ allocates memory (rarely overridden).
  2. __init__ initializes attributes.
  3. __repr__ / __str__ render human-readable info.
  4. __del__ runs when garbage collected (avoid heavy logic).

Identity vs equality

a = Invoice(100, "Ada")
b = Invoice(100, "Ada")
print(a is b) # False
print(a == b) # Depends on __eq__

Mutable objects share references; copy them when needed.

Next up in your learning path