Instance & Class Attributes
Attributes are the variables attached to a class or an instance. They represent the state of an object. In Python, attributes are incredibly dynamic, but understanding the difference between “belonging to the class” and “belonging to the instance” is vital.
1. Instance Attributes
Section titled “1. Instance Attributes”These are unique to each individual object. They are almost always defined inside the __init__ method.
class User: def __init__(self, username): self.username = username
u1 = User("Alice")u2 = User("Bob")# u1.username is different from u2.username2. Class Attributes
Section titled “2. Class Attributes”These are shared by all instances of a class. They are defined directly in the class body.
class Airplane: wings = 2 # Every airplane has 2 wings
def __init__(self, model): self.model = model
a1 = Airplane("747")a2 = Airplane("A380")
print(a1.wings) # 2print(a2.wings) # 2The Shadowing Trap
Section titled “The Shadowing Trap”If you try to change a class attribute through an instance, you accidentally create a new instance attribute that hides (shadows) the class attribute for that one object.
a1.wings = 3 # This creates a NEW 'wings' attribute on a1 only!print(Airplane.wings) # Still 23. Dynamic Attribute Access: getattr and setattr
Section titled “3. Dynamic Attribute Access: getattr and setattr”Sometimes you don’t know the name of the attribute until the program is running (e.g., reading from a config file).
attr_name = input("Which attribute to check? ") # e.g., 'model'val = getattr(a1, attr_name, "Not found")print(val)
setattr(a1, "color", "White")4. Under the Hood: Memory & __slots__
Section titled “4. Under the Hood: Memory & __slots__”By default, every Python object stores its attributes in a dictionary (__dict__). Dictionaries are fast, but they consume a lot of RAM because they are “over-allocated” to handle growth.
If you are creating millions of small objects (like points in a 3D simulation), the memory overhead of __dict__ can be massive. You can optimize this using __slots__.
class Point: # This tells Python: "Only these two attributes exist. # Do NOT create a __dict__ for this class." __slots__ = ("x", "y")
def __init__(self, x, y): self.x = x self.y = yBenefits of __slots__:
Section titled “Benefits of __slots__:”- Memory: Significantly reduces the RAM usage per object.
- Speed: Attribute access is slightly faster.
- Safety: Prevents users from accidentally adding new, misspelled attributes (e.g.,
p.z = 10would raise anAttributeError).
5. Summary Table
Section titled “5. Summary Table”| Attribute Type | Scope | Storage |
|---|---|---|
| Instance | Unique to the object. | instance.__dict__ |
| Class | Shared by all objects. | Class.__dict__ |
| Slots | Fixed & Optimized. | Fixed memory offsets. |