understanding self in python class
4. Understanding self
What is self?
self is a reference to the current object (instance) of a class.
When you call a method using an object, Python automatically passes that object as the first argument to the method.
Example
class Student:
def show(self):
print(self)
s1 = Student()
s1.show()
Output
<__main__.Student object at 0x123456>
Key Point
self is the object that called the method.
What Python Does Internally
When you write:
s1.show()
Python internally does something similar to:
Student.show(s1)
The object (s1) is automatically passed as the first argument.
Therefore:
def show(self):
becomes:
def show(s1):
for that particular method call.
Notes
*
Why Do We Need self?
Suppose we create two objects.
class Student:
def __init__(self, name):
self.name = name
s1 = Student("John")
s2 = Student("Alice")
Python stores data separately for each object.
Memory Representation
s1
└── name = John
s2
└── name = Alice
Key Point
self tells Python which object's data should be used.
Notes
*
How self Works Inside __init__
Example:
class Student:
def __init__(self, name):
self.name = name
When:
s1 = Student("John")
Python internally performs something similar to:
Student.__init__(s1, "John")
Inside the method:
self = s1
name = "John"
Then:
self.name = name
becomes:
s1.name = "John"
Key Point
self.name stores data inside the object.
Notes
*
Visual Representation
Creating first object:
s1 = Student("John")
Memory:
s1
│
├── name = John
Creating second object:
s2 = Student("Alice")
Memory:
s1
│
├── name = John
s2
│
├── name = Alice
Key Point
Each object has its own copy of instance variables.
Notes
*
Accessing Variables Using self
Example:
class Student:
def __init__(self, name):
self.name = name
def display(self):
print(self.name)
s1 = Student("John")
s1.display()
Output
John
Explanation
Inside display():
self.name
means:
s1.name
because self refers to the object that called the method.
Notes
*
Accessing Methods Using self
Methods inside a class can call other methods using self.
Example
class Student:
def greet(self):
print("Hello")
def display(self):
self.greet()
print("Welcome")
s1 = Student()
s1.display()
Output
Hello
Welcome
Explanation
self.greet()
internally becomes:
Student.greet(self)
If self refers to s1:
Student.greet(s1)
Key Point
Use self.method_name() to call another method of the same object.
Notes
*
What Happens If We Don't Use self?
Incorrect Example
class Student:
def __init__(self, name):
name = name
Problem
This only creates a local variable.
After the constructor finishes, the variable disappears.
Nothing is stored inside the object.
Notes
*
Correct Usage
class Student:
def __init__(self, name):
self.name = name
Now the value is stored inside the object.
s1 = Student("John")
print(s1.name)
Output
John
Notes
*
Most Important Rule
Whenever data should belong to an object:
self.variable_name
Examples:
self.name
self.age
self.salary
self.balance
Whenever you want to call another method of the same object:
self.method_name()
Examples:
self.display()
self.save()
self.calculate()
Notes
*
Real-Life Analogy
Imagine:
s1 = Student("John")
s2 = Student("Alice")
Think of self as asking:
"Which student am I currently talking about?"
For s1:
self.name
means:
John
For s2:
self.name
means:
Alice
The same code works for every object because self always points to the current object.
Notes
*
Interview Question
Q: Is self a keyword in Python?
Answer: No.
You can technically use another name.
Example:
class Student:
def show(my_object):
print(my_object)
Python will still work.
However, using self is the official convention and should always be followed.
Recommended Style
class Student:
def show(self):
print(self)
Notes
*
Summary
selfrefers to the current object.- Python automatically passes the object as the first argument.
- Use
self.variableto store and access object data. - Use
self.method()to call another method in the same object. - Without
self, values are local variables and disappear after method execution. selfis not a Python keyword, but it is the standard convention.
Revision Checklist
- What is
self? - How Python passes
self - Accessing variables using
self - Calling methods using
self - Why
self.variableis required - Interview question on
self