Instance Variables and Instance Methods in Python
Source: Dev.to
Instance Variables & Instance Methods in Python
In Object‑Oriented Programming (OOP) the two concepts you must master are:
- Instance Variables
- Instance Methods
If these aren’t clear, OOP will always feel confusing. Let’s break everything down step‑by‑step in the simplest way possible.
A Real‑Life Analogy
Class = a classroom blueprint (the teacher).
Objects = the students.
Each student:
- Has a name
- Has a roll number
- Has marks
- Can perform actions (submit homework, give a test, etc.)
Even though all students come from the same class, each student has its own data.
- That “own data” = Instance Variables
- The actions they perform = Instance Methods
What Is an Instance Method?
An instance method is a function that belongs to an object and works on that specific object.
Definition (in simple words)
- Defined inside a class
- Takes
selfas 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
user1 = User()
user2 = User()
Calling the method:
user1.activate()
Python actually executes:
User.activate(user1) # self == user1
Thus, instance methods always operate on the object that calls them.
What Is an Instance Variable?
An instance variable is a variable that belongs to a specific object.
It is created by assigning to an attribute of self:
self.variable_name = value
Key Points
- Stores data
- Is unique for each object
- Exists inside the object
Example
def set_email(self, email):
self.email = email
user1.set_email("shameel@hasabtech.com")
After the call:
user1.email = "shameel@hasabtech.com"
email now belongs only to user1.
user2 still has no email attribute until we set it.
Complete Example with Deep Explanation
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()
- Python creates two separate objects in memory.
- Each object gets its own space to store data.
- Both objects can use all instance methods.
Note: Object and instance mean the same thing.
What Happens Inside Memory?
Even though user1 and user2 share the same class blueprint, they have independent storage for their instance variables. They do not share those variables.
Using Instance Methods
user1.set_email("shameel@hasabtech.com")
- Python passes
user1automatically asself. - Inside the method
self.email = emailbecomesuser1.email = "shameel@hasabtech.com".
Now:
user1has anemailattribute.user2still has none.
Adding More Data
user1.activate()
user2.deactivate()
Resulting attributes:
user1.is_active = True
user2.is_active = False
Each object’s is_active flag is independent, illustrating the core idea of instance variables.
Why Are They Called Instance Methods?
- They work with instance variables.
- Their behavior depends on the object that calls them.
- They use
selfto access that object’s data.
def show_status(self):
print(f"{self.email} is {'active' if self.is_active else 'not active'}")
Calling user1.show_status() prints the status for user1, while user2.show_status() (once email and is_active are set) prints the status for user2.
Common Beginner Confusion
Is self a keyword?
No. self is just a naming convention. You could name it anything:
def activate(myobject):
myobject.is_active = True
However, by convention the first parameter of an instance method is always called self.
Why This Concept Is Crucial
Understanding instance variables and instance methods lets you:
- Build login systems
- Create user‑management tools
- Design any application that needs multiple independent objects with their own data and behavior
Master these fundamentals, and OOP will stop feeling confusing!
Ment 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.
selfis 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