Copied to Clipboard
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?
- Python sees user1
- It passes user1 automatically as self
- 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):
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