When learning Python's object-oriented programming, one of the first concepts you'll encounter is the mysterious self keyword. If you're new to classes and methods, understanding self is vital for writing clear, functional, and idiomatic Python code.
In this lesson, we'll unravel the role of self, explore why it's necessary, and see it in action through practical examples. By the end, you will confidently know how to use self to work with instance attributes and methods.
What Exactly Is self?
self is a conventional name for the first parameter of instance methods in Python classes. It represents the instance of the class itself. When you create an object from a class, self allows you to access that particular object's attributes and methods.
Think of self as a way for an object to refer to itself internally. It's like saying, "This object here, right now — that's me."
💡 Why "self"?
Python does not use the this keyword like some other programming languages (Java, C++, JavaScript). Instead, it explicitly passes the instance as the first argument to instance methods, and by convention, we name that parameter self. This explicitness might seem verbose at first, but it increases code readability and transparency.
How Does self Work in Practice?
Let's start with a simple class example to illustrate where self fits in:
📌 Deep Dive: Basic Class Using self
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self):
print(f"{self.name} says: Woof!")
# Create an instance of Dog
my_dog = Dog("Buddy", 4)
my_dog.speak()
Here’s what’s happening step-by-step:
__init__is the initializer method that runs when a newDogobject is created. Notice it always takesselfas the first argument.self.name = namesets an attributenameon the particular object instance.speakis an instance method that also takesself, so it can access the object's own attributes.- When we call
my_dog.speak(), Python automatically passes the instancemy_dogasself.
💡 Remember: You never explicitly pass self when calling instance methods. Python handles it for you behind the scenes.
Why Does Python Require self?
Unlike some languages where the instance is implied, Python's design philosophy favors explicitness. This means you must explicitly include self in method definitions so you can clearly access instance variables and other methods.
Imagine methods without self:
- How would Python differentiate between local variables and instance variables?
- How would you call other methods or change the object's state?
By requiring self, Python makes your intentions clear and your code easier to understand and reason about.
Common Mistakes with self
⚠️ Forgetting to include self as the first parameter
One of the most frequent errors beginners make is omitting self in method definitions. This leads to unexpected errors when calling instance methods.
📌 Deep Dive: What Happens if You Forget self?
class Cat:
def __init__(name, age): # Missing 'self' here
self.name = name
self.age = age
kitty = Cat("Whiskers", 3)
Because Python implicitly passes the instance as the first argument, the method definition must accept it. Omitting self causes a mismatch in arguments.
⚠️ Using a different name instead of self
While self is just a naming convention (you could use any valid variable name), it's highly recommended to stick with self. Using other names can confuse readers and lead to less maintainable code.
How self Works Internally
When you call a method on an instance, Python translates:
instance.method(args)
into:
Class.method(instance, args)
This means the instance itself is passed as the first argument to the method — that's why self must be explicitly declared to receive it.
Using self to Access and Modify Attributes
self allows you not only to read but also to update instance attributes, which lets each object maintain its own state independently.
📌 Deep Dive: Modifying Attributes with self
class Car:
def __init__(self, brand, speed=0):
self.brand = brand
self.speed = speed
def accelerate(self, amount):
self.speed += amount
print(f"{self.brand} is now going {self.speed} km/h.")
my_car = Car("Toyota")
my_car.accelerate(30)
my_car.accelerate(20)
Here, self.speed is updated independently for each object instance, demonstrating how self is essential for managing object state.
Distinguishing self from Class and Static Methods
In Python, not all methods receive self. There are two special types:
- Class methods, which receive
clsas the first parameter and operate on the class itself, not instances. - Static methods, which don't receive any implicit first argument and behave like regular functions inside the class namespace.
| Method Type | First Parameter | Accesses |
|---|---|---|
| Instance Method | self | Instance attributes and methods |
| Class Method | cls | Class attributes and methods |
| Static Method | None | Neither instance nor class data unless explicitly passed |
Understanding when and why self is used will help you design your classes properly and understand Python’s object model.
Visualizing self in the Object Architecture

Summary: Key Takeaways About self
selfalways refers to the instance of the class.- It must be the first parameter of every instance method.
- You do not pass
selfexplicitly when calling methods; Python does it automatically. - Using
selfallows you to access and modify instance attributes and call other instance methods. - It is a naming convention, but you should always stick to the name
selffor readability.
💡 Think of self as a bookmark.
Whenever your program deals with multiple objects, self is like a bookmark pointing to the current page — so you always know exactly which object you’re working with.
Next Steps
Now that you understand self, you can confidently create classes with instance methods that interact with object-specific data. This opens the door to more advanced topics such as inheritance, encapsulation, and polymorphism.
Try creating your own classes and experiment with self by adding attributes and methods — practice is the best way to solidify your understanding!
Quick Knowledge Check
Test what you just learned
Question 1 of 2
What does the self keyword represent inside a Python class method?
Question 2 of 2
What happens if you forget to include self as the first parameter in an instance method?
Loading results...