Last modified: Nov 26, 2026
Python init and self: A Clear Guide
If you are learning Python classes, you will meet __init__ and self very fast. They look strange at first. But they are simple once you see them in action.
This guide explains both ideas step by step. You will see real code and real output. By the end, you will know why every class example seems to use them.
What Is a Class in Python?
A class is a blueprint. It describes the data and behavior of an object. An object is a real thing built from that blueprint.
Think of a class as a cookie cutter. The cookies are objects. Each cookie has the same shape but can have its own flavor.
You create a class with the class keyword. You create methods inside it with the def keyword. If you want to understand that keyword better, this guide on Python def and how it works covers it well.
What Is self in Python?
self is a reference to the current object. It lets a method read and change that object's data.
When you call a method on an object, Python passes that object as the first argument. The name self is just a convention. But you should always use it, because every Python developer expects it.
Here is a simple example:
class Dog:
def bark(self):
# self points to this specific dog object
print("Woof! I am", self.name)
dog = Dog()
dog.name = "Rex"
dog.bark()
Woof! I am Rex
Notice that bark takes self but we call it with no arguments. Python fills in self for us. That is the key rule to remember.
What Is __init__ in Python?
__init__ is a special method. Python calls it automatically when you create a new object. Its job is to set up the object's starting data.
People often call it the constructor. That is close enough for daily use. If you want the full details, this explanation of the Python __init__ method goes deeper.
Here is a class with a proper __init__:
class Dog:
def __init__(self, name, age):
# runs automatically when a Dog is created
self.name = name
self.age = age
def describe(self):
print(f"{self.name} is {self.age} years old.")
dog = Dog("Rex", 3)
dog.describe()
Rex is 3 years old.
When you write Dog("Rex", 3), Python creates a new object. Then it calls __init__ with that object as self, plus your two arguments.
How self and __init__ Work Together
The two names solve two problems. self answers "which object?" and __init__ answers "what setup does a new object need?"
Inside __init__, self is the new object. Every line like self.name = name stores data on that object. After setup, the object is ready to use.
This pairing is the heart of object-oriented Python. Once it clicks, most class code becomes easy to read.
Why Is self Required?
Python does not hide the current object. It passes it openly. This makes the code explicit and easy to follow.
It also means you can have many objects at once. Each call gets its own self. So two dogs never mix up their names.
class Counter:
def __init__(self, start=0):
self.value = start
def add(self, amount):
self.value += amount
return self.value
a = Counter()
b = Counter(10)
a.add(5)
b.add(5)
print(a.value, b.value)
5 15
Each object keeps its own value. That is only possible because self points to a specific object on every call.
Common Mistakes With self and __init__
Beginners often forget self in the method definition. Python then raises a TypeError about missing arguments. The fix is simple: add self as the first parameter.
Another common mistake is writing __init__ with the wrong number of underscores. It must have two on each side. One underscore does nothing special.
A third mistake is trying to return a value from __init__. Python expects it to return None. If you return something else, Python raises a TypeError.
Finally, some people create a class with no __init__ at all. That is allowed. Python just skips the setup step. If you are curious about that case, this article on a Python class without __init__ explains when it is fine.
Default Values and Validation in __init__
You can give __init__ parameters default values. This makes objects easier to create. You can also validate input right away.
class Account:
def __init__(self, owner, balance=0):
if balance < 0:
raise ValueError("Balance cannot be negative")
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount
acc = Account("Ana")
acc.deposit(50)
print(acc.owner, acc.balance)
try:
Account("Bo", -10)
except ValueError as e:
print("Error:", e)
Ana 50
Error: Balance cannot be negative
Validating in __init__ keeps bad objects from being created. That is a strong habit to build early.
Class Variables vs Instance Variables
Data set through self belongs to one object. It is an instance variable. Data set on the class itself is shared by all objects.
class User:
platform = "web" # shared by all users
def __init__(self, name):
self.name = name # unique per user
u1 = User("Sam")
u2 = User("Lee")
print(u1.platform, u2.platform)
print(u1.name, u2.name)
web web
Sam Lee
Use self for data that changes per object. Use the class for data that stays the same. Mixing them up causes confusing bugs.
Quick Summary
__init__ runs when an object is created. It sets up the object. self is the reference to that object inside methods.
Always put self first in instance methods. Do not return values from __init__. Use clear names and validate input when it matters.
Conclusion
__init__ and self are the foundation of Python classes. __init__ prepares a new object. self tells each method which object it is working on.
Start with small classes. Add one attribute at a time. Run the code and watch the output. Soon the pattern will feel natural, and you will write cleaner, safer object-oriented Python.