An object is a concrete instance created from a class. You can make many objects from one class, and each carries its own state. Understanding the difference between what's shared (class attributes) and what's per-object (instance attributes) is key.
Many objects, one class
Each instance has independent instance attributes; a class attribute is shared by every instance:
class Circle:pi = 3.14159 # class attribute — shared by ALL circlesdef __init__(self, r):self.r = r # instance attribute — unique per circledef area(self):return Circle.pi * self.r ** 2a = Circle(2)b = Circle(5)a.area() # 12.566… uses a's rb.area() # 78.539… uses b's ra.r, b.r # (2, 5) independent state
Identity
Each object is distinct in memory. == compares values (if defined); is checks whether two names point at the same object:
c1 = Circle(2)c2 = Circle(2)c1 is c2 # False — different objects, even with the same radiusc1 is c1 # True
Key takeaways
- 01An object is an instance of a class with its own independent state.
- 02Instance attributes (self.x) are per-object; class attributes are shared by all.
- 03Create instances by calling the class like a function.
- 04is checks identity (same object); == checks value equality.
- 05Avoid mutable class attributes — they are shared across every instance.
