Last modified: Nov 27, 2026

Python Self Parameter in Classes

The self parameter is one of the first confusing topics for new Python developers. You see it in almost every method. But what does it actually do?

This guide explains self in plain language. You will see clear examples, real output, and the rules that matter.

What Is the Self Parameter?

Self is a reference to the current instance of a class. It lets a method read and change data that belongs to a specific object.

Python passes the instance automatically when you call a method. You do not type it yourself at the call site.

Think of self as the word "me" inside a class. Each object uses self to talk about its own data.

Why Does Python Use Self?

Python does not hide the instance reference like some other languages do. It makes the link explicit.

This design keeps methods simple. A method always knows which object it works on.

It also means you can call a method in two ways. One way goes through the object. The other way goes through the class and passes the object by hand.


class Dog:
    def bark(self):
        print("Woof from", self.name)

dog = Dog()
dog.name = "Rex"

dog.bark()        # Called through the object
Dog.bark(dog)     # Called through the class

Woof from Rex
Woof from Rex

Both calls do the same thing. The first call passes dog as self behind the scenes. The second call passes it openly.

Self in the __init__ Method

The __init__ method runs when you create an object. It sets up the starting attributes.

Here self matters a lot. It stores values on the new instance, not on the class.


class Car:
    def __init__(self, brand, speed):
        self.brand = brand    # Stored on the instance
        self.speed = speed

car = Car("Toyota", 120)
print(car.brand)
print(car.speed)

Toyota
120

Each new car keeps its own brand and speed. The self prefix is what makes that separation work.

If you want a deeper look at how construction works, read this guide on Python init and self: A Clear Guide. It connects the two ideas step by step.

What Happens If You Forget Self?

If you leave out self in a method definition, Python still passes the instance. The call fails because the argument count is wrong.


class Cat:
    def meow():          # Missing self
        print("Meow")

cat = Cat()
cat.meow()

TypeError: Cat.meow() takes 0 positional arguments but 1 was given

This error confuses many beginners. The method looks fine. But Python quietly passes cat as the first argument.

The fix is simple. Add self as the first parameter.

Self Is a Convention, Not a Keyword

You can name the first parameter anything. Python does not care.


class Bird:
    def fly(me):         # Works, but not recommended
        print("Flying")

Bird().fly()

Flying

Still, always use self. Other developers expect it. Tools and linters expect it too.

The same naming rule applies to the constructor. This article on the Python __init__ Method Explained shows why clear names help when classes grow.

Self and Instance vs Class Attributes

Attributes set with self belong to one object. Attributes set on the class belong to all objects.


class Counter:
    total = 0            # Class attribute

    def __init__(self):
        self.count = 0   # Instance attribute

    def add(self):
        self.count += 1
        Counter.total += 1

a = Counter()
b = Counter()
a.add()
a.add()
b.add()

print(a.count, b.count)
print(Counter.total)

2 1
3

Notice the difference. Each object has its own count. The total is shared.

Mixing these up causes subtle bugs. Use self for per-object data. Use the class name for shared data.

Self in Methods That Return Self

Some methods return self. This supports method chaining.


class Builder:
    def __init__(self):
        self.parts = []

    def add(self, part):
        self.parts.append(part)
        return self      # Return the same object

    def show(self):
        print(self.parts)

Builder().add("a").add("b").show()

['a', 'b']

Returning self keeps the chain alive. Each call works on the same object.

This pattern appears in many libraries. It is also useful when you write your own fluent APIs.

Self in Decorated Methods

Decorators wrap methods. The self value still flows through as the first argument.


def log(func):
    def wrapper(self, *args, **kwargs):
        print("Calling", func.__name__)
        return func(self, *args, **kwargs)
    return wrapper

class Task:
    @log
    def run(self):
        print("Running task")

Task().run()

Calling run
Running task

The wrapper must accept self and pass it along. Otherwise the call breaks.

For more patterns like this, see the Python Class Decorator Guide. It covers method wrapping in detail.

Common Mistakes with Self

Most self errors come from a few simple slips. Watch for these.

First, forgetting self in the parameter list. Second, forgetting self. when reading an attribute inside a method.

Third, using a class attribute where an instance attribute is needed. Fourth, passing extra arguments that Python already fills.


class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def show(self):
        print(x, y)      # Wrong: NameError

p = Point(1, 2)
p.show()

NameError: name 'x' is not defined

The fix is to write self.x and self.y. The method must reach attributes through the instance.

Self and Inheritance

In a subclass, self refers to the actual object, not the parent class. This makes shared methods work on child objects.


class Animal:
    def speak(self):
        print(self.sound)

class Cow(Animal):
    sound = "Moo"

Cow().speak()

Moo

The parent method uses self.sound. The child provides the value. This is a small example of polymorphism in action.

Conclusion

The self parameter links a method to the object that called it. Python passes it for you, so you only need to declare it.

Use self to store and read instance data. Keep the name self for clarity. Watch the common mistakes, and your classes will work smoothly.

Once this idea clicks, the rest of object-oriented Python becomes much easier to learn.