Instance Methods¶
What is an Instance Method?¶
An instance method is a method that operates on a specific object (instance) of a class.
It receives the instance as its first parameter, conventionally named self.
Because it has access to the object, an instance method can read and modify that object's state.
A Simple Example¶
This looks straightforward.
But something interesting happens here.
The method definition is
which expects one argument.
Yet we call
without passing anything.
So where did self come from?
What Actually Happens?¶
When Python sees
it automatically passes the instance.
Conceptually,
Both are equivalent.
Python performs this method binding automatically.
Understanding self¶
self is not a keyword.
It is simply the first parameter that receives the current object.
This works too.
Although valid,
using anything other than self is strongly discouraged because self is the accepted Python convention.
Memory Representation¶
Class
User
│
greet(self)
│
------------------------------------
user
│
▼
+-----------------------+
| name = "Gaurav" |
| age = 25 |
+-----------------------+
Calling
binds the object to the method.
Inside the method,
Both user and self reference the same instance.
Verifying It¶
Output
Both IDs are identical.
This proves that self and user reference the same object.
Why Do We Need self?¶
Suppose a class stores user information.
Create two users.
Calling
prints
Calling
prints
How does Python know which name to print?
Because self points to the instance that invoked the method.
Conceptually,
Bound Methods¶
When we access
notice that we didn't call the method.
There are no parentheses.
What is stored inside method?
Let's check.
Output
Python creates a bound method.
A bound method is simply a method that already remembers which object it belongs to.
Conceptually,
It stores two things.
Now calling
is equivalent to
The instance has already been remembered.
Bound Method Visualization¶
Calling
automatically supplies
Accessing the Method from the Class¶
Now try
Notice the difference.
No object exists.
So Python returns the function itself.
Calling
raises an error.
Because no instance was provided.
Instead,
works perfectly.
Instance Methods Can Access Instance Data¶
The method modifies the current object's state.
When Should You Use Instance Methods?¶
Use instance methods whenever the behavior depends on the state of a specific object.
Examples include:
- Updating account balance
- Changing user profile
- Validating object state
- Reading instance attributes
Where are instance methods stored?¶
Instance methods are stored on the class, not on each object.
When accessed through an instance, Python creates a bound method that combines the function from the class with the current instance.
This design avoids storing a separate copy of every method for each object, making Python classes memory efficient.
Common Interview Questions¶
What is an instance method?¶
An instance method is a method that operates on a specific object. It automatically receives the instance as its first argument (self), allowing it to access and modify that object's state.
What is self?¶
self is a reference to the current instance of the class. Python automatically passes it when an instance method is invoked through an object.
Is self a keyword?¶
No. self is just a naming convention. You can technically use any name, but self is the universally accepted convention in Python.
How is self passed automatically?¶
When you access a method through an instance, Python creates a bound method. The bound method stores the instance and automatically passes it as the first argument when invoked.
Conceptually,
becomes
What is a bound method?¶
A bound method is a method object that remembers the instance it belongs to. When called, Python automatically supplies that stored instance as self.
Why does User.greet() raise an error?¶
Because greet() expects an instance as its first argument. Calling it directly from the class doesn't provide self, so Python raises a TypeError.
Correct usage:
When should you use an instance method?¶
Use an instance method whenever the behavior depends on the state of a particular object. If the method needs to read or modify instance attributes, it should be an instance method.
Can an instance method access class variables?¶
Yes. An instance method can access both instance variables and class variables through self or the class itself.
Can an instance method be called without creating an object?¶
Yes, but you must explicitly pass an instance.
This is exactly what Python does internally when you write:
Why are methods defined inside the class instead of the object?¶
Methods represent behavior that is common to all instances of a class. Storing them on the class avoids duplicating the same function for every object, making memory usage more efficient.
Common Interview Mistakes¶
❌ Thinking self is a keyword.
✔ It is simply the first parameter representing the current instance.
❌ Thinking Python creates a copy of the object for self.
✔ self references the exact same object.
❌ Thinking instance methods belong to the object.
✔ Methods are defined on the class.
Objects receive bound methods when accessed through an instance.
Interview Tip¶
If asked,
How does Python automatically pass self?
A strong answer is:
"When an instance method is accessed through an object, Python creates a bound method. The bound method stores both the original function and the instance. When the bound method is invoked, Python automatically passes the stored instance as the first argument (
self). Conceptually,obj.method()is equivalent toClass.method(obj)."
Key Takeaways¶
- Instance methods operate on a specific object.
selfis a reference to the current instance.selfis passed automatically by Python.obj.method()is conceptually equivalent toClass.method(obj).- Accessing a method through an instance creates a bound method.
- A bound method remembers both the function and the instance.
- Instance methods are used whenever behavior depends on object state.