Python Instance Variables
Table of Contents
In the last lesson, we learned about Python Constructors. After learning about constructors, let us learn about instance variables in Python. Instance variables are variables that belong to a specific object.
In this tutorial, we will learn what instance variables are, how to create them, and how they differ from class variables.
What Are Instance Variables in Python?
Instance variables are variables that belong to a specific instance (object) of a class. Each object has its own copy of instance variables.
They are usually created inside the __init__ method using self.
class Dog: def __init__(self, name, age): self.name = name self.age = age
dog1 = Dog("Buddy", 3)dog2 = Dog("Max", 5)
print(dog1.name) # Buddyprint(dog2.name) # MaxOutput
BuddyMaxHere, name and age are instance variables. Each dog has its own name and age.
Why Do We Need Instance Variables in Python?
Instance variables let each object have its own data. Without them, all objects would share the same data. That would be useless.
For example, if all dogs had the same name, we could not tell them apart. Instance variables let us give each dog a different name.
How to Create Instance Variables in Python
We create instance variables by assigning to self inside a method.
class Dog: def __init__(self, name): self.name = name
def set_age(self, age): self.age = age
dog = Dog("Buddy")dog.set_age(3)
print(dog.name)print(dog.age)Output
Buddy3We can also create instance variables outside the class.
class Dog: pass
dog = Dog()dog.name = "Buddy"dog.age = 3
print(dog.name)print(dog.age)Output
Buddy3But it is better to create them in the constructor. That way they are always set.
How to Access Instance Variables in Python
We access instance variables using self inside the class, and using the object name outside the class.
class Dog: def __init__(self, name): self.name = name
def bark(self): print(f"{self.name} says Woof!")
dog = Dog("Buddy")dog.bark()print(dog.name)Output
Buddy says Woof!BuddyInside the class, we use self.name. Outside, we use dog.name.
How to Modify Instance Variables in Python
We can change instance variables at any time.
class Dog: def __init__(self, name): self.name = name
dog = Dog("Buddy")print(dog.name)
dog.name = "Max"print(dog.name)Output
BuddyMaxThat is useful when we want to update data.
A Real-Life Example – Student Class with Instance Variables
class Student: def __init__(self, name, age, course): self.name = name self.age = age self.course = course
def introduce(self): print(f"Hi, I am {self.name}. I am {self.age} years old. I study {self.course}.")
student1 = Student("Alex", 20, "Computer Science")student2 = Student("Sam", 22, "Mathematics")
student1.introduce()student2.introduce()Output
Hi, I am Alex. I am 20 years old. I study Computer Science.Hi, I am Sam. I am 22 years old. I study Mathematics.Each student has their own instance variables. That is the power of instance variables.
Instance Variables vs Class Variables in Python
Instance variables belong to each object. Class variables belong to the class.
We will learn about class variables in the next tutorial. For now, just remember that instance variables are unique to each object.
Common Mistakes with Instance Variables in Python
Mistake 1: Forgetting self
class Dog: def __init__(self, name): name = name # This does not create an instance variableThis creates a local variable, not an instance variable.
Fix:
self.name = nameMistake 2: Accessing instance variables without an object
class Dog: def __init__(self, name): self.name = name
print(Dog.name)This gives an error. Because name is an instance variable. It only exists on objects.
Fix:
dog = Dog("Buddy")print(dog.name)Mistake 3: Sharing instance variables between objects
class Dog: pass
dog1 = Dog()dog1.name = "Buddy"
dog2 = Dog()print(dog2.name)This gives an error. Because dog2 does not have a name attribute. Instance variables are not shared.
Fix: Set the attribute for each object.
dog2.name = "Max"Next, we will learn about Python Class Variables and share one value across every object of a class.