1. Introduction
self is the conventional name for the first parameter of every instance method in a Python class. It represents the specific object the method is being called on, giving the method access to that object's attributes and other methods. Without self, a method would have no way to know which instance's data to read or modify.
Cricket analogy: Just as a specific batter's stance is unique to them and not the whole team, self lets a method like score_run() know it's Kohli's innings data being updated, not some generic player's.
When you write obj.method(arg), Python automatically passes obj as the first argument to method, which is why the method's definition includes self as its first parameter even though you don't pass it explicitly at the call site.
Cricket analogy: When you call kohli.bat(ball), it's as if the scorer automatically writes "Kohli" on the scorecard before recording the shot — Python passes kohli into self the same way.
2. Syntax
class ClassName:
def __init__(self, value):
self.value = value # self refers to the instance being created
def show(self):
return self.value # self accesses this instance's attribute
obj = ClassName(10)
obj.show() # equivalent to ClassName.show(obj)3. Explanation
Every regular method defined inside a class must declare self as its first parameter. When called via obj.method(args), Python's attribute lookup mechanism (the descriptor protocol) automatically binds obj to self, so the call becomes equivalent to ClassName.method(obj, args). This is how a method knows exactly which object's attributes to operate on.
Cricket analogy: The umpire's decision review system automatically links a review request to the specific batter under review, just as Python's descriptor protocol binds the calling player (obj) to self when kohli.review() is called.
Inside a method, self.attr = value creates or updates an attribute on that specific instance, and self.method() calls another method on the same instance. This is how objects maintain and manipulate their own independent state.
Cricket analogy: When Kohli's method updates self.runs += 4, it's like the scorer crediting the boundary only to Kohli's individual tally, and self.celebrate() is Kohli calling on his own bat-raise routine, not another batter's.
self is not a reserved keyword in Python — it is purely a naming convention. You could technically name the first parameter anything (e.g., this, obj), and the code would still work, because Python binds the instance to whatever name is listed first. However, every Python developer expects self, and deviating from it makes code confusing and violates PEP 8.
Forgetting to include self as the first parameter of an instance method — or forgetting to pass it when calling via the class (ClassName.method(obj)) — is a common source of TypeError: missing 1 required positional argument errors. Static methods (@staticmethod) are the exception: they intentionally omit self because they don't operate on a specific instance.
4. Example
class Counter:
def __init__(self, start=0):
self.count = start
def increment(self, step=1):
self.count += step
return self.count
c1 = Counter()
c2 = Counter(100)
print(c1.increment())
print(c1.increment(5))
print(c2.increment(10))
# Calling the method explicitly through the class, passing the instance manually
print(Counter.increment(c1, 2))
print("this_name_works_too:", (lambda that: that.count)(c1))5. Output
1
6
110
8
this_name_works_too: 86. Key Takeaways
- self refers to the specific instance a method is operating on.
- obj.method(args) is automatically translated to ClassName.method(obj, args) by Python.
- self is a naming convention, not a reserved keyword — but you should always follow it.
- Inside a method, self.attr reads or writes that instance's own attribute.
- Static methods deliberately omit self because they don't act on a particular instance.
Practice what you learned
1. What does self represent inside an instance method?
2. Is self a reserved keyword in Python?
3. What is obj.method(args) automatically translated to by Python?
4. In the Counter example, what does Counter.increment(c1, 2) return, given c1.count was 6 beforehand?
5. Which kind of method intentionally omits self?
Was this page helpful?
You May Also Like
Constructors in Python
How __init__ initializes new objects, why it isn't really the constructor, and how default values work.
Classes and Objects in Python
How Python's class statement defines a blueprint for objects, and how instances are created and used.
Class Methods and Static Methods in Python
The difference between instance methods, @classmethod (receives cls), and @staticmethod (receives neither self nor cls), with practical use cases.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics