π§© 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 Method | Class Method |
|---|---|
First parameter = self | First parameter = cls |
| Works on instance data | Works on class-level data |
| Requires object to call | Can 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! π