Instance Variables

Instance variables vary from object to object and are declared in the constructor in source They’re more common:

class Wall:
    def __init__(self) -> None:
        self.height = 10  # instance variable (per object)
 
 
south_wall = Wall()
south_wall.height = 20  # only updates this instance of a wall
print(south_wall.height)
# prints "20"
 
north_wall = Wall()
print(north_wall.height)
# prints "10"

Class Variables

Class variables are shared between instances of the same class and are declared at the top level of a class definition in source They’re less common:

class Wall:
    height: int = 10  # class variable (shared across all instances)
 
 
south_wall = Wall()
print(south_wall.height)
# prints "10"
 
Wall.height = 20  # updates all instances of a Wall
 
print(south_wall.height)
# prints "20"