🧩 Python OOP β€” Instance Methods

Introduction 🌟

An Instance Method is a function defined inside a class that operates on **instance attributes** (data belonging to a specific object). These methods always take self as their first parameter.

Note

πŸ’‘ Think of an instance method as β€œa behavior an object can perform.”

1. Basic Example of Instance Method 🧱

basic_instance_method.py

class Person:
    def greet(self):              # instance method
        print("Hello!")

p = Person()
p.greet()

βœ” Instance methods must be called using an object.
βœ” self is automatically passed by Python.

2. Instance Methods with Instance Attributes πŸ”—

method_with_attributes.py

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

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

p = Person("Sathish")
p.greet()

βœ” Methods use instance attributes through self.

3. Instance Methods That Return Values 🎯

return_value.py

class Calculator:
    def add(self, a, b):
        return a + b

c = Calculator()
print(c.add(10, 20))

4. Modifying Instance Attributes Inside Methods πŸ”§

modify_attributes.py

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

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

acc = BankAccount(500)
acc.deposit(200)
print(acc.balance)  # 700

βœ” Instance methods can change the object's internal state.

5. Calling Instance Methods from Other Instance Methods πŸ”

calling_methods.py

class Player:
    def __init__(self, name):
        self.name = name

    def intro(self):
        return f"Player: {self.name}"

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

p = Player("Arun")
p.greet()

βœ” Objects can chain behaviors using instance methods.

6. Instance Methods vs Class Methods βš”οΈ

Instance MethodClass Method
First parameter = selfFirst parameter = cls
Works on instance dataWorks on class-level data
Requires object to callCan be called using class or object

7. Example: Student System πŸŽ’

student_instance_method.py

class Student:
    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

    def details(self):
        return f"{self.name} scored {self.grade}"

s = Student("Sathish", 95)
print(s.details())

8. Example: Ecommerce Product πŸ›οΈ

product_example.py

class Product:
    def __init__(self, title, price):
        self.title = title
        self.price = price

    def discount(self, percent):
        return self.price - (self.price * percent / 100)

p = Product("Phone", 30000)
print(p.discount(10))

9. Best Practices πŸ’‘

  • βœ” Always include self as the first parameter
  • βœ” Keep methods focused on one purpose
  • βœ” Use instance methods to manipulate instance attributes
  • βœ” Use descriptive method names (verbs: run, start, add, remove)

10. Advanced Example: Password Manager πŸ”

password_manager.py

class User:
    def __init__(self, username, password):
        self.username = username
        self.password = password
    
    def change_password(self, new_password):
        self.password = new_password
        print("Password updated!")

u = User("admin", "1234")
u.change_password("newpass")

βœ” Instance methods modify sensitive object data safely.

Conclusion πŸŽ‰

>>β€œInstance methods define WHAT an object can do β€” they give life and behavior to your classes.” ✨

You now fully understand Instance Methods in Python! Want the next topic? Try Class Methods, Static Methods, Constructors, or Inheritance. Just tell me! 😊