Skip to content

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.

Beginner 8 min read·Lesson 165 of 306 in Python Programming

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

Python
class Counter:
def __init__(self):
self.count = 0 # attribute of this object
def click(self):
self.count += 1 # update this object
return self.report() # call another method on it
def report(self):
return f"count={self.count}"
  • 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

Python
class Counter:
def __init__(self, name):
self.name = name
self.count = 0
def click(self):
self.count += 1
print(f"{self.name}: {self.count}")
a, b = Counter("a"), Counter("b")
a.click(); a.click(); b.click()
Output
a: 1
a: 2
b: 1

Showing that self is the object

Python
class Probe:
def me(self):
return self
p = Probe()
print(p.me() is p, Probe.me(p) is p)
Output
True True

More examples

Errors caused by missing self

Python
class Bad:
def greet():
return "hi"
class Worse:
def __init__(self):
self.total = 0
def add(self, x):
total = self.total + x # local variable, not the attribute!
try:
Bad().greet()
except TypeError as e:
print("TypeError:", e)
w = Worse()
w.add(5)
print("total is still", w.total)
Output
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:

TextOutput
TypeError: Bad.greet() takes 0 positional arguments but 1 was given
total is still 0

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

def reset(self):
count = 0

Prefer

def reset(self):
self.count = 0

Without self., the assignment creates a local variable that disappears after the call.

Passing self explicitly

Avoid

acc.deposit(acc, 100)

Prefer

acc.deposit(100)

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

  1. 1
    Write a Player class with a level-up method that updates level and health through self.
  2. 2
    Find 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?
Practise this topic