Skip to main content

Command Palette

Search for a command to run...

Instance Variables and Instance Methods in Python

Learn How Instance Variables and Methods Actually Work in Python

Published
5 min read
Instance Variables and Instance Methods in Python

In Object-Oriented Programming (OOP), two of the most important concepts are:

  • Instance Variables

  • Instance Methods

If you don’t understand these properly, OOP will always feel confusing.

So let’s break everything down step by step in the simplest way possible.

Let’s Start with a Simple Real-Life Example

Imagine a classroom. Learn How Instance Variables and Methods Actually Work in Python

You (teacher) are the class blueprint.
Your students are objects.

Every student:

  • Has a name

  • Has a roll number

  • Has marks

  • Can perform actions (submit homework, give test, etc.)

Now here’s the important part:

Even though all students come from the same class,
each student has their own data.

That “own data” is exactly what we call:

Instance Variables

And the actions they perform?

Instance Methods

What is an Instance Method in Python?

An instance method is a method that belongs to an object and works on that specific object.

Definition (Simple Words)

An instance method:

  • Is defined inside a class

  • Has self as its first parameter

  • Can access and modify instance variables

    Why Do We Need self?

When we create multiple objects from a class, each object has its own data.

self tells Python:

Work with the current object that is calling this method

Example

class User:
    def activate(self):
        self.is_active = True

If we create two objects:

user1 = User()
user2 = User()

When we call

user1.activate()

Python automatically does this internally:

User.activate(user1)

That means:

  • self = user1

So instance methods always operate on the object that calls them

What is an Instance Variable in Python?

An instance variable is a variable that belongs to a specific object.

It is created using:

self.variable_name = value

Important Concept

Instance variables:

  • Store data

  • Are unique for each object

  • Exist inside the object

  • Can be different for different objects

Example

def set_email(self, email):
    self.email = email

When we call:

user1.set_email("shameel@hasabtech.com")

It becomes:

user1.email = "shameel@hasabtech.com"

Now email belongs only to user1.

If we check user2, it still does NOT have email until we set it.

That is why instance variables are object-specific.

Complete Example with Deep Explanation

Let’s build a proper example and understand what happens step by step.

class User:

    def activate(self):
        self.is_active = True

    def deactivate(self):
        self.is_active = False

    def set_email(self, email):
        self.email = email

    def show_status(self):
        print(f"{self.email} is {'active' if self.is_active else 'not active'}")

Step 1: Creating Objects (Instances)

user1 = User()
user2 = User()

When we execute this:

  • Python creates two separate objects in memory

  • Each object has its own space to store data

  • Both objects have access to all instance methods

Important: Object and Instance mean the same thing.

What Happens Inside Memory?

Even though both objects come from the same class blueprint:

  • user1 has its own data storage

  • user2 has its own data storage

They do NOT share instance variables.

This is the beauty of OOP.

Understanding Instance Methods in Action

Now let’s call:

user1.set_email("shameel@hasabtech.com")

What happens?

  1. Python sees user1

  2. It passes user1 automatically as self

  3. Inside method:

    self.email = email
    

    Becomes

user1.email = "shameel@hasabtech.com"

Now:

  • user1 has email

  • user2 still has nothing

This proves instance variables belong to objects.

Adding More Data to Objects

Now:

user1.activate()
user2.deactivate()

This creates:

user1.is_active = True
user2.is_active = False

Again:

  • Each object has its own is_active

  • They are completely independent

This independence is the core idea of instance variables.

Why Methods are Called Instance Methods?

Because they:

  • Work with instance variables

  • Depend on the object

  • Use self to access object data

For example:

def show_status(self):
    print(f"{self.email} is {'active' if self.is_active else 'not active'}")

This method:

  • Reads self.email

  • Reads self.is_active

  • Prints data of that specific object

So this method behaves differently for each object.

That’s why it is called an instance method.

Common Beginner Confusion

Many students think:

Is self a keyword?

No.

self is just a naming convention.

You can write:

def activate(myobject):

But by convention, we always write self

Why This Concept is Very Important?

If you understand instance variables and instance methods, you can:

  • Build login systems

  • Create user management systems

  • Understand Django models

  • Work with APIs

  • Design real-world applications

Without understanding self, OOP will feel complicated.

Final Understanding

Whenever:

  • You attach data using self → It becomes an instance variable.

  • You define a method with self → It becomes an instance method.

Each object:

  • Has its own data

  • Shares method structure

  • Behaves independently

Summary

  • Object = Instance. They mean the same thing.

  • Instance Variables are the "adjectives" (data) that describe the object.

  • Instance Methods are the "verbs" (actions) the object can perform.

  • self is the bridge that connects the method to the specific object’s data.

  • Each object has its own copy of instance variables

Stay connected with hasabTech for more information:

Website | Facebook | LinkedIn | YouTube | X (Twitter) | TikTok

Python OOP Made Easy (Beginner Friendly Series)

Part 1 of 5

Object Oriented Programming in Python is a step by step learning series for beginners and intermediate developers, covering classes, objects, inheritance, encapsulation, polymorphism, and real-world Python examples.

Up next

Understanding self in Python OOP: A Beginner Friendly Guide

What is self in Python? A Simple Explanation with Real-World Examples for Beginners