a man writing coding with his laptop
Photo by Danial Igdery on Unsplash

Understanding Instance Variables in Python (Made Simple!)

Mastering Instance Variables in Python: A Beginner’s Guide

Coding with Harish
3 min read18 hours ago

--

If you’ve just started learning Python and object-oriented programming (OOP), you might have heard the term instance variables. I’ll break it down for you in the simplest way possible!

What Are Instance Variables in Python?

Instance variables are variables that belong to an instance of a class. Each object created from a class has its own separate copy of these variables. Unlike class variables, which are shared across all instances, instance variables store unique data for each object.

Think of it like this: If you and I both enroll in an online Python course, we each have our own login credentials. The course (class) is the same, but our usernames (instance variables) are unique!

How to Define Instance Variables in Python

To define an instance variable, you use the self keyword inside a class. Let’s look at a simple example:

class Student:
def __init__(self, name, age):
self.name = name # Instance variable
self.age = age # Instance variable
# Creating instances
student1 = Student("Harish", 20)
student2 = Student("Chandru", 22)
print(student1.name) # Output: Harish
print(student2.age) # Output: 22

In this example, name and age are instance variables. Each Student object has its own values for these variables.

Where Are Instance Variables Stored?

Instance variables are stored inside the __dict__ attribute of an object. You can check this using:

print(student1.__dict__)

This will output:

{'name': 'Harish', 'age': 20}

Each instance has its own __dict__, storing only its specific data.

Difference Between Instance Variables and Class Variables

Feature Instance Variables Class Variables Defined inside __init__() method Outside __init__() Belongs to Individual instances Entire class Changes affect Only one instance All instances

Example of a class variable:

class Course:
category = "Programming" # Class variable

def __init__(self, name):
self.name = name # Instance variable
course1 = Course("Python Basics")
course2 = Course("Java Basics")
print(course1.category) # Output: Programming
print(course2.category) # Output: Programming

Modifying Instance Variables

You can modify instance variables for a specific object like this:

student1.age = 21  # Changing Harish's age
print(student1.age) # Output: 21

This change won’t affect other instances like student2.

To learn more about accessing and modifying instance variables, check out PYnative.

FAQs About Instance Variables

1. What happens if I access an instance variable that doesn’t exist?

Python will raise an AttributeError:

print(student1.grade)  # AttributeError: 'Student' object has no attribute 'grade'

2. Can instance variables be created outside __init__?

Yes! You can add them later:

student1.grade = "A"
print(student1.grade) # Output: A

3. How do I delete an instance variable?

Use del:

del student1.age
print(student1.age) # Raises AttributeError

Master Python Faster With My Free Resources!

Want to solidify your Python basics? Download my free Python notes PDF and enroll in my free Python crash course here: Join Now.

Personalized 1-to-1 Session

Struggling with a specific Python topic? Let’s work through it together! Book a free 30-minute 1-to-1 session with me, and we’ll tackle any Python concept you’re struggling with. Book your session here.

--

--

Coding with Harish
Coding with Harish

Written by Coding with Harish

Coding with Harish simplifies complex coding concepts. Join me to learn and build your programming skills.

No responses yet