Core Concept Mastery • Python Visual Series Module
Instance variables are attributes that belong to a specific object. Each object created from a class gets its own independent copy of instance variables. Changing one object's data does not affect another.
class Dog:
def __init__(self, name):
self.name = name
self.tricks = []
a = Dog("Rex")
b = Dog("Buddy")
a.tricks.append("roll over")
print(a.name, a.tricks)
print(b.name, b.tricks)
Dog's __init__ creates two instance variables on every new object: "name" (from the argument) and "tricks" (an empty list). Each object gets its own independent copies.