Instance Variables vs Class Variables in Python
An instance variable belongs to one specific object, set (usually inside __init__()) with self.name = value — every instance gets its own independent copy. A class variable belongs to the class itself, defined directly inside the class body outside any method, and is shared by every instance unless a particular instance overrides it.
Instance variables use self
self.name = value, typically set inside __init__().
Each instance has its own copy
Instance variables are never shared between objects.
Class variables live in the class body
Written at the same indentation as method definitions, outside any method.
Class variables start out shared
Until one instance is given its own instance variable of the same name.
Assigning through an instance shadows, not shares
instance.variable = value creates a new instance variable rather than changing the class-wide one.
class Dog: species = "Canis familiaris" def __init__(self, name): self.name = name rex = Dog("Rex")bella = Dog("Bella") print(rex.species, bella.species) rex.species = "Wolf (just for Rex)"print(rex.species, bella.species)Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.