Instance vs Class Attributes
Differentiate between per-object and shared class-level data.
Introduction
Understanding the difference between instance and class attributes prevents subtle bugs and enables efficient shared state.
Class Attribute
class Dog: legs = 4 — legs is shared by all instances. Access as Dog.legs or dog.legs.
class Dog:
legs = 4
print(Dog.legs)
print(Dog().legs)All lessons in this course
- Classes and the __init__ Method
- Instance vs Class Attributes
- Instance Methods and self
- Magic Methods: __str__ and __repr__