🏷️ 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 AttributeClass Attribute
Unique per objectShared by all objects
Defined in __init__()Defined outside methods
Stored in objectStored 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! 😊