DEV Community

Shameel Uddin
Shameel Uddin

Posted on • Originally published at blog.hasab.tech

Understanding self in Python OOP: A Beginner Friendly Guide

One of the biggest "roadblocks" for Python beginners is the self keyword. You see it in almost every class, it’s the first argument in every method, and yet, we don't seem to pass any value to it when we call the function.

If you’ve ever wondered:

  • What does self actually do?
  • Why do I have to type it every single time?
  • How does Python know which object I’m talking about?

Then this Blog is for you. Let’s break it down.

What is a Class and a Method?

Before we talk about self, remember the relationship between a Class and an Object.

  • Class: A blueprint (like a drawing of a house).
  • Object (Instance): The actual house built from that blueprint.

If you build five houses from the same blueprint, they all have the same structure, but they have different owners and different furniture inside. self is the way the house "identifies" itself.

class User:
    def set_email(self, email):
        self.email = email
Enter fullscreen mode Exit fullscreen mode

Here:

  • User → Class
  • set_email() → Method

What is self? (The Simple Definition)

selfrepresents the specific instance (object) of the class.

When you define a method inside a class, Python needs a way to distinguish between different objects.

class Robot:
    def introduce(self):
        print(f"Hello, I am {self.name}")
Enter fullscreen mode Exit fullscreen mode

In the code above, self.name tells Python: Look for the name attribute inside THIS specific robot, not all robots in the world.

The Secret Behind Method Calls

This is where most beginners get confused. Look at these two ways to do the exact same thing:

Method 1: The Manual Way (Internal)

User.set_email(user1, "ammad@hasab.tech")
User.set_email(user2, "shameel@hasab.tech")
Enter fullscreen mode Exit fullscreen mode

In this way:

  • We manually pass the object
  • user1 becomes self
  • This helps us understand what Python does internally

Method 2: The Pythonic Way (Standard)

user1.set_email("ammad@hasab.tech")
user2.set_email("shameell@hasab.tech")
Enter fullscreen mode Exit fullscreen mode

Behind the scenes, Python converts this into:

User.set_email(user1, "ammad@hasab.tech")
Enter fullscreen mode Exit fullscreen mode

That’s why:

  • This method is simpler and mostly used in real projects.

How self Works with Different Objects

When this line runs:

user1.set_email("ammad@hasab.tech")
Enter fullscreen mode Exit fullscreen mode
  • selfuser1
  • user1.email"ammad@hasab.tech"

When this line runs:

user2.set_email("shameel@hasab.tech")
Enter fullscreen mode Exit fullscreen mode
  • selfuser2
  • user2.email"shameel@hasab.tech"

This shows that self ensures each user instance uses it’s own data or value, which is the core idea of Object Oriented Programming.

Why self Is Important

self allows Python to:

  • Identify which object is calling the method
  • Store data separately for each object
  • Avoid data overwriting between objects

Without self, Python would not know where to store object-specific values.

Common Beginner Mistakes

  • Thinking self is a keyword (it’s not, but it’s a strong convention)
  • Forgetting to add self as the first parameter
  • Confusing instance variables with class variables

Always remember:

self.variable_name
Enter fullscreen mode Exit fullscreen mode

means the variable belongs to that specific object.

Summary

  • self represents the current object in Python
  • It helps Python identify which object is calling a method
  • A method can be called in two ways:
  • Using class name (manual)
  • Using object name (automatic and common)
  • self changes depending on the object calling the method
  • It allows each object to maintain its own data independently

Understanding self is a foundation of Python OOP, and once you grasp it, many advanced concepts become much easier.

Stay connected with hasabTech for more information:
Website | Facebook | LinkedIn | YouTube | X (Twitter) | TikTok

Top comments (0)