πŸ™‹β€β™‚οΈ Python OOP β€” The self Parameter

Introduction 🌟

The self parameter is one of the core components of Python’s Object-Oriented Programming. It represents the **current object** (instance) and allows access to its attributes and methods. Without self, an object wouldn't know its own data!

Note

πŸ’‘ self is NOT a keyword β€” it’s just a naming convention. You can rename it, but you should never do that.

1. Why Do We Need self? πŸ€”

Every object has its own data. The self parameter allows instance methods to refer to **that specific object's attributes**.

why_self.py

class Person:
    def __init__(self, name):
        self.name = name   # self refers to the current object

p1 = Person("Sathish")
p2 = Person("Kumar")

print(p1.name)  # Sathish
print(p2.name)  # Kumar

βœ” Each object keeps its own values because of self.

2. How Python Passes self Automatically 🧠

auto_pass_self.py

class Demo:
    def show(self):
        print("Method called")

d = Demo()
d.show()          # Python calls Demo.show(d) internally

βœ” You NEVER pass self manually.
βœ” Python injects it automatically when calling methods.

3. Using self to Access Instance Attributes πŸ”—

access_attributes.py

class Car:
    def __init__(self, model, year):
        self.model = model
        self.year = year

    def info(self):
        print(f"Model: {self.model}, Year: {self.year}")

c = Car("BMW", 2023)
c.info()

βœ” self.model & self.year belong to that specific car object.

4. Using self to Modify Instance Attributes πŸ› οΈ

modify_attributes.py

class Bank:
    def __init__(self, balance):
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

b = Bank(500)
b.deposit(200)
print(b.balance)

βœ” Methods can update object state using self.

5. Calling Other Methods Using self πŸ”

call_other_method.py

class Player:
    def intro(self):
        return "I am a player"

    def greet(self):
        print("Hello!", self.intro())

p = Player()
p.greet()

βœ” self lets methods interact with each other inside the same object.

6. Why self Must Be the First Parameter ⚠️

Python needs consistency. The first parameter of any instance method must ALWAYS refer to the object calling that method.

why_first_param.py

class Demo:
    def show(self):
        print(self)

d = Demo()
d.show()   # prints the memory address of object 'd'

7. self Inside __init__ (Constructor) πŸš€

self_in_init.py

class User:
    def __init__(self, username, password):
        self.username = username
        self.password = password

u = User("admin", "1234")
print(u.username)

βœ” Constructor uses self to assign initial attributes.

8. You *Can* Rename self, BUT You Shouldn’t πŸ˜…

rename_self.py

class Test:
    def show(obj):
        print("Hello")

t = Test()
t.show()

Note

βœ” Technically works
❌ But VERY bad practice β€” never rename self.

9. Instance Attribute Storage Using self.__dict__ πŸ“¦

dict_attributes.py

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

p = Person("Sathish", 23)
print(p.__dict__)

βœ” All instance attributes stored in a dictionary internally.

10. Real-World Example: Shopping Cart πŸ›’

shopping_cart.py

class Cart:
    def __init__(self):
        self.items = []

    def add_item(self, item):
        self.items.append(item)

c = Cart()
c.add_item("Laptop")
c.add_item("Mouse")

print(c.items)

βœ” self stores each object's unique shopping cart items.

11. Best Practices πŸ’‘

  • βœ” Always name the first parameter self
  • βœ” Use self only for instance-specific data
  • βœ” Never store unrelated data inside the same object
  • βœ” Use self to clearly distinguish instance variables from local variables

Conclusion πŸŽ‰

>>β€œThe self parameter gives identity to objects β€” it tells each object who it is and what data it owns.” ✨

You now fully understand the self Parameter in Python! Want the next topic? Try Constructors, Encapsulation, Magic Methods, or Inheritance. Just tell me! 😊