Skip to main content

Command Palette

Search for a command to run...

Understanding self in Python OOP: A Beginner Friendly Guide

What is self in Python? A Simple Explanation with Real-World Examples for Beginners

Updated
3 min read
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

Here:

  • User → Class

  • set_email() → Method

What is self? (The Simple Definition)

self represents 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}")

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")

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")

Behind the scenes, Python converts this into:

User.set_email(user1, "amma@hasab.tech")

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")

When this line runs:

user2.set_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

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

Python OOP Made Easy (Beginner Friendly Series)

Part 2 of 5

Object Oriented Programming in Python is a step by step learning series for beginners and intermediate developers, covering classes, objects, inheritance, encapsulation, polymorphism, and real-world Python examples.

Up next

Understanding Classes and Objects in Python

A beginner-friendly guide to understanding Python classes, objects, instances, and real-world OOP concepts