π·οΈ Python OOP β Instance Attributes
Introduction π
**Instance Attributes** are variables that belong to a specific object (instance) of a class. Each object gets its own copy of these attributes, meaning changes in one object do NOT affect others.
Note
π‘ Instance attributes are always created inside the __init__() method using self.
1. Basic Example of Instance Attributes π§±
instance_basic.py
class Person:
def __init__(self, name, age):
self.name = name # instance attribute
self.age = age # instance attribute
p1 = Person("Sathish", 23)
p2 = Person("Kumar", 30)
print(p1.name, p1.age)
print(p2.name, p2.age)β Each object stores its own name and age
2. How Instance Attributes Work π
- β Defined inside __init__()
- β Accessed using self.attribute
- β Unique to each object
- β Stored in objectβs __dict__
dict_example.py
print(p1.__dict__)
print(p2.__dict__)β Shows attributes stored inside each instance.
3. Modifying Instance Attributes π οΈ
modify_instance.py
class Car:
def __init__(self, model, year):
self.model = model
self.year = year
c = Car("BMW", 2020)
c.year = 2023 # modifying instance attribute
print(c.year)β Changes apply only to that object.
4. Adding New Instance Attributes Dynamically π§©
dynamic_attributes.py
class Dog:
def __init__(self, name):
self.name = name
d = Dog("Tommy")
d.age = 5 # dynamically adding attribute
print(d.name, d.age)Note
β Python allows adding attributes anytime to any object.
5. Instance Attributes vs Class Attributes βοΈ
instance_vs_class.py
class Student:
school = "Govt School" # class attribute
def __init__(self, name):
self.name = name # instance attribute
s1 = Student("Arun")
s2 = Student("Kumar")
Student.school = "Public School" # changes for ALL students
s1.name = "Arun Kumar" # changes only this object
print(s1.name, s1.school)
print(s2.name, s2.school)| Instance Attribute | Class Attribute |
|---|---|
| Unique per object | Shared by all objects |
Defined in __init__() | Defined outside methods |
| Stored in object | Stored in class |
6. Instance Methods Using Instance Attributes π§©
methods_with_attributes.py
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
r = Rectangle(5, 10)
print(r.area())β Instance methods operate on instance attributes.
7. Real-World Example: Bank Account π°
bank_account.py
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount
acc1 = BankAccount("Sathish", 5000)
acc2 = BankAccount("Arun", 3000)
acc1.deposit(2000)
print(acc1.owner, acc1.balance)
print(acc2.owner, acc2.balance)β Each account has its own owner and balance.
8. Best Practices π‘
- β Always initialize instance attributes inside __init__()
- β Use clear and descriptive attribute names
- β Avoid storing unnecessary values in every instance
- β Access instance attributes only through self
Conclusion π
>>βInstance attributes make each object unique β they store the data that defines an individual instance.β β¨
You now fully understand Instance Attributes in Python! Want the next topic? Try Class Attributes, Methods, Constructors, or Inheritance. Just tell me! π