Module 11 · Object-Oriented Programming — topic 8 of 25
self
What self is, why Python passes it explicitly, using self to reach attributes and other methods, and common errors when self is missing.
Learning objectives
- Explain what self refers to in a method call
- Use self to access attributes and call other methods
- Diagnose errors caused by a missing self
Definition
self is the conventional name of the first parameter of an instance method. It refers to the particular object the method was called on, giving the method access to that object's attributes and other methods.
Introduction
When two Counter objects call click(), the same method code runs, but each call updates a different counter. self is how the method knows which object it is working on.
Why it matters
Missing self is the most common error in beginner classes. Understanding self also makes the link between methods and functions crystal clear.
Detailed explanation
In a.click(), Python passes a as the first argument, so inside the method self is a. In b.click(), self is b.
self.attribute reads or writes the attribute on that specific object; a plain name like count would be a local variable instead.
self.other_method() calls another method on the same object.
The name self is a convention, not a keyword; any name works, but always use self so others can read your code.
Python makes self explicit (unlike this in Java) in keeping with 'explicit is better than implicit': you can always see where an attribute comes from.
Syntax
- self is the object the method was called on.
- Use self. for attributes and methods.
- self must be the first parameter.
Code examples
self is the calling object
a: 1 a: 2 b: 1
Showing that self is the object
True True
More examples
Errors caused by missing self
TypeError: Bad.greet() takes 0 positional arguments but 1 was given total is still 0
Step-by-step explanation
- greet has no parameter for the instance, but Python passes one anyway: TypeError.
- In Worse.add, total = ... creates a local variable; the attribute self.total is never updated.
- No error is raised in the second case, which makes it harder to spot — the result is simply wrong.
Expected output
Running “Errors caused by missing self” prints exactly:
Practical applications
- Every method in every Python framework uses self (or cls for class methods).
Best practices
- Always name the first instance-method parameter self.
- Prefix attribute access with self. inside methods.
- Read 'self.x' as 'this object's x'.
Common mistakes
Assigning to a local instead of an attribute
Avoid
Prefer
Without self., the assignment creates a local variable that disappears after the call.
Passing self explicitly
Avoid
Prefer
Python supplies self automatically when you call through the object.
Important notes
- Class methods receive the class as cls instead of self; static methods receive neither.
Key takeaways
- self is the object a method was called on.
- Use self.attr and self.method() inside methods.
- Missing self causes TypeErrors or silent local variables.
Try it yourself
- 1Write a Player class with a level-up method that updates level and health through self.
- 2Find and fix the missing self in a given broken class.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat happens when C().hello() runs?