What is self in Python? Explained for Beginners

In Python, the keyword self is one of the most confusing things for beginners.

You see it inside every method of a class… but you never pass it manually when calling the method.

So, what is self? Why is it required? And how should you use it?

Let’s clear it up with a detailed explanation, step-by-step examples, and analogies.


🧠 What is self?

In Python, self refers to the current object (instance) of the class.

It allows:

  • Accessing variables tied to that object
  • Calling other methods inside the same object
  • Storing unique data per object

🧠 Think of it like:

self is how your object talks to itself.”


🔧 Syntax: Always the First Parameter

Python
class Person:
    def greet(self):  # ← `self` is required
        print("Hello!")

Even though you don’t pass it explicitly when calling the method…

Python
p = Person()
p.greet()   # self is passed automatically

…Python behind the scenes does this:

Python
Person.greet(p)  # 'p' becomes self

✅ Why Is self Needed?

Let’s add attributes:

Python
class Person:
    def __init__(self, name):
        self.name = name

    def greet(self):
        print(f"Hello, I’m {self.name}")

Python
p1 = Person("Ali")
p2 = Person("Fatima")

p1.greet()  # Hello, I’m Ali
p2.greet()  # Hello, I’m Fatima

self.name allows each object to store its own name.

Without self, every object would be identical which defeats the purpose of OOP.


🧪 Real-Life Analogy

Think of a class as a form, and an object as a filled form.

  • self = this specific form
  • You write your own name, address, etc. in the form (unique per object)

🚫 Common Mistake: Forgetting self

Python
class Car:
    def drive():   # ❌ Missing self
        print("Driving...")

This will throw an error:

Python
TypeError: drive() takes 0 positional arguments but 1 was given

✅ Fix:

Python
class Car:
    def drive(self):
        print("Driving...")

✅ Best Practices

RuleWhy?
Always use self in instance methodsPython automatically passes it for you
Name it exactly self (not this)It’s a strong Python convention (not enforced by syntax)
Use self.variable in methodsKeeps data tied to the current object

💌 Stay Updated with PyUniverse

Want Python and AI explained simply straight to your inbox?

Join hundreds of curious learners who get:

  • ✅ Practical Python tips & mini tutorials
  • ✅ New blog posts before anyone else
  • ✅ Downloadable cheat sheets & quick guides
  • ✅ Behind-the-scenes updates from PyUniverse

No spam. No noise. Just useful stuff that helps you grow one email at a time.

🛡️ I respect your privacy. You can unsubscribe anytime.

Leave a Comment