Last modified: Nov 27, 2026
Python Self Parameter Example
The self parameter is one of the first things beginners notice in Python classes. It looks strange at first. But it plays a simple and important role.
This guide explains self with clear examples. You will see real code and real output. By the end, you will know why self exists and how to use it.
What Is the self Parameter?
self is a reference to the current instance of a class. It lets a method access the object's own data.
When you call a method on an object, Python passes that object as the first argument. The name self is just a convention. Python does not require that exact name, but you should always use it.
If you want a deeper look at how this works inside class bodies, read about the Python self parameter in classes.
A Simple Example
Let's start with a small class. It stores a name and prints a greeting.
class Greeter:
def __init__(self, name):
self.name = name # store the name on the object
def greet(self):
print(f"Hello, {self.name}!") # use self to read the name
# Create an object
g = Greeter("Alice")
g.greet()
Here is the output.
Hello, Alice!
Notice two things. The __init__ method takes self as its first parameter. The greet method takes self too.
But when we call g.greet(), we pass no arguments. Python fills in self automatically with the object g.
Why Does self Exist?
A class is a blueprint. An object is a real thing built from that blueprint. You can build many objects from one class.
Each object needs its own data. self tells Python which object's data to use.
Imagine two greeter objects. Each has a different name. The method must know which name to print. That is the job of self.
a = Greeter("Alice")
b = Greeter("Bob")
a.greet() # self is a
b.greet() # self is b
Hello, Alice!
Hello, Bob!
The same method prints different results. The only difference is the object passed as self.
self and the __init__ Method
The __init__ method runs when you create an object. It sets up the object's starting data.
Every instance method needs self as its first parameter. This includes __init__. For a full walkthrough, see the guide on Python init and self.
Here is a class that stores a price and applies a discount.
class Product:
def __init__(self, name, price):
self.name = name # set the product name
self.price = price # set the product price
def discount(self, percent):
# self.price belongs to this object
return self.price - (self.price * percent / 100)
p = Product("Laptop", 1000)
print(p.discount(10))
900.0
The method reads self.price. That value belongs to the object p. If you create another product, it gets its own price.
To understand exactly what __init__ does at creation time, read the Python __init__ method explained.
What Happens If You Forget self?
This is a very common beginner error. If you leave self out of a method, Python raises a TypeError.
class Dog:
def __init__(self, name):
self.name = name
def bark(): # missing self
print("Woof!")
d = Dog("Rex")
d.bark()
TypeError: Dog.bark() takes 0 positional arguments but 1 was given
Python passed the object d to bark. But bark has no parameter to receive it. So the call fails.
The fix is simple. Add self as the first parameter.
def bark(self):
print("Woof!")
self Is Not a Keyword
self is not a reserved word in Python. You can name it anything.
class Cat:
def __init__(this, name):
this.name = name # works, but avoid this style
def meow(this):
print(this.name, "says meow")
c = Cat("Tom")
c.meow()
Tom says meow
This code runs fine. But do not do it. self is the standard. Other Python developers expect it. Using another name makes your code harder to read.
self in Different Method Types
Not every method needs self. The rule depends on the method type.
Instance methods take self. They work on one object.
Class methods take cls instead. They work on the class itself.
Static methods take neither. They are plain functions placed inside a class.
class Circle:
pi = 3.14159 # class attribute
def __init__(self, radius):
self.radius = radius
def area(self): # instance method
return Circle.pi * self.radius ** 2
@classmethod
def unit_circle(cls): # class method
return cls(1)
@staticmethod
def describe(): # static method
return "A circle is round"
print(Circle.unit_circle().area())
print(Circle.describe())
3.14159
A circle is round
Learning how decorators change method behavior is useful here. A Python class decorator guide can help you go further.
Common Mistakes With self
Here are the mistakes beginners make most often.
Forgetting self in the method definition. This causes a TypeError. Always make self the first parameter of instance methods.
Passing self when calling a method. Do not write obj.method(self). Python handles it for you. Just write obj.method().
Using self outside a class.self only makes sense inside methods. A plain function does not need it.
Forgetting self when reading attributes. Inside a method, write self.name, not name. Without self, Python looks for a local variable and fails.
class User:
def __init__(self, name):
self.name = name
def show(self):
print(name) # wrong: missing self
u = User("Sam")
u.show()
NameError: name 'name' is not defined
The fix is print(self.name). The attribute lives on the object, so you must reach it through self.
self and Object Identity
You can prove that self is the same object you called the method on. Use id() to compare.
class Box:
def check(self):
print("self id:", id(self))
b = Box()
print("object id:", id(b))
b.check()
object id: 140234567890
self id: 140234567890
The numbers match. This confirms that self points to the exact object that called the method.
Quick Summary of Rules
Keep these rules in mind. They cover almost every case you will meet.
Put self first in every instance method. Use self.attribute to read or set object data. Do not pass self when calling methods. Keep the name self for readability.
Follow these rules and your classes will work correctly. Most self-related errors disappear once the pattern becomes a habit.
Conclusion
The self parameter connects a method to the object that called it. It is how Python knows which object's data to use.
You saw it in __init__ and in regular methods. You saw what happens when it is missing. You also learned that self is a convention, not a keyword.
Practice with small classes. Store one or two attributes. Call a method. Watch how self carries the object through. Soon the pattern will feel natural, and Python classes will become much easier to write.