What is the self Keyword in Python?
Understand the self keyword in Python: how it refers to the current instance, why it is passed automatically, and how it manages per-object data.
Expected Interview Answer
self is the conventional name for the first parameter of an instance method; it refers to the specific object the method is being called on, letting the method read and modify that object's own attributes.
When you call obj.method(), Python automatically passes obj as the first argument, which the method receives as self. Inside the method you use self.attribute to access or set data unique to that instance, distinguishing it from other objects of the same class. The name self is a strong convention, not a reserved keyword — any name would work — but everyone uses self for readability.
- Gives each instance access to its own data
- Distinguishes one object's state from another's
- Lets methods read and update instance attributes
- Makes the object being acted on explicit
- Keeps class code readable through a shared convention
AI Mentor Explanation
Imagine each batter fills in a personal scorecard. When a batter says 'add a run to my score', 'my' points to their own card, not a teammate's. self is that 'my': it tells the method exactly whose runs, whose average and whose stats to update.
Step-by-Step Explanation
Step 1
Declare self first
Every instance method lists self as its first parameter, e.g. def deposit(self, amount):, even though callers do not pass it explicitly.
Step 2
Python passes the object
When you write account.deposit(100), Python automatically supplies account as self, so account.deposit(100) is really Account.deposit(account, 100).
Step 3
Access instance data
Inside the method use self.attribute to read or update data unique to that object, like self.balance += amount.
Step 4
Set attributes in __init__
In the constructor, self.name = name binds the argument to this specific instance so each object keeps its own values.
Step 5
Remember it is a convention
self is not a reserved word; any valid name works, but using self keeps code consistent and readable for everyone.
What Interviewer Expects
- self refers to the current instance
- Python passes it automatically on method calls
- self.attribute accesses per-object data
- Awareness that self is convention, not a keyword
- Clear distinction between instance and class
Common Mistakes
- Forgetting to include self in a method definition
- Thinking self is a reserved Python keyword
- Passing self manually when calling a method
- Confusing self (instance) with cls (class in classmethods)
- Assigning a local variable instead of self.attribute and losing state
Best Answer (HR Friendly)
“self is how a method inside a class refers to the specific object it is working on, like saying 'my own data'. It lets each object keep and update its own information without mixing it up with other objects of the same type.”
Code Example
class Account:
def __init__(self, owner, balance=0):
self.owner = owner # this object's owner
self.balance = balance # this object's balance
def deposit(self, amount):
self.balance += amount # update THIS account
return self.balance
a = Account("Asha", 100)
b = Account("Ben", 50)
a.deposit(25)
print(a.balance) # 125 (only a changed)
print(b.balance) # 50
# a.deposit(25) is really Account.deposit(a, 25)Follow-up Questions
- What is the difference between self and cls?
- Why does Python make self explicit instead of implicit like other languages?
- Can you name the first parameter something other than self?
- What happens if you forget self in a method definition?
- How does self relate to instance versus class attributes?
MCQ Practice
1. What does self refer to inside an instance method?
self is the specific object the method was called on, giving access to that instance's attributes.
2. How is self supplied when you call obj.method()?
Python translates obj.method() into Class.method(obj), so obj becomes self automatically.
3. Which statement about self is true?
self is not reserved; any name works, but the convention is universally followed for readability.
Flash Cards
What is self? — The first parameter of an instance method that refers to the object the method is called on.
Is self a keyword? — No — it is a naming convention. Any valid identifier works, but everyone uses self.
How is self passed? — Automatically: obj.method(x) becomes Class.method(obj, x), so obj is bound to self.
self vs cls? — self is the instance in instance methods; cls is the class in classmethods decorated with @classmethod.