πŸ—οΈ Python OOP β€” Classes & Objects

Introduction 🌟

In Python, **Classes** and **Objects** form the foundation of Object-Oriented Programming (OOP). A **class** is a blueprint, and an **object** is an instance of that blueprint. OOP helps you build scalable, reusable, and organized applications.

Note

πŸ’‘ Think of a *class* as a β€œtemplate” and an *object* as the β€œreal thing” created from that template.

1. Creating Your First Class 🧱

basic_class.py

class Person:
    pass

p = Person()     # creating object
print(p)

βœ” Person is a class
βœ” p is an object of that class

2. Adding Attributes & Methods ✨

class_attributes_methods.py

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

    def greet(self):
        print(f"Hello, my name is {self.name}.")

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

βœ” __init__() runs automatically when object is created
βœ” self refers to the current object
βœ” name & age are **instance attributes**

3. Understanding self 🧠

self represents the object itself β€” it stores data belonging to that specific object.

self_explanation.py

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

c1 = Car("BMW")
c2 = Car("Audi")

print(c1.model)  # BMW
print(c2.model)  # Audi

Note

βœ” Each object keeps its own data.

4. Class Attributes vs Instance Attributes 🏷️

class_vs_instance.py

class Student:
    school = "Govt School"   # class attribute

    def __init__(self, name):
        self.name = name      # instance attribute

s1 = Student("Arun")
s2 = Student("Kumar")

print(Student.school)
print(s1.name, s2.name)
Class AttributeInstance Attribute
Shared by all objectsUnique to each object
Defined outside methodsDefined inside __init__()

5. Object Methods (Behaviors) 🧩

object_methods.py

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

c = Calculator()
print(c.add(5, 3))

6. The __init__() Method (Constructor) πŸ”§

This special method runs automatically when creating an object.

constructor.py

class Book:
    def __init__(self, title):
        print("Book created:", title)

b = Book("Python Mastery")

7. Multiple Objects from One Class 🧱

multiple_objects.py

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

d1 = Dog("Tommy")
d2 = Dog("Bruno")

print(d1.name, d2.name)

8. Adding Methods That Modify Object State πŸ”„

modify_state.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

9. String Representation (__str__) πŸ“

str_method.py

class Person:
    def __str__(self):
        return "This is a Person class"

p = Person()
print(p)

βœ” Makes objects print-friendly.

10. Real-World Example: Student System πŸŽ’

student_example.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())

11. Real-World Example: Ecommerce Product πŸ›οΈ

product_example.py

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

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

p = Product("Laptop", 50000)
print(p.discount(10))

12. Best Practices πŸ’‘

  • βœ” Use meaningful class names (CamelCase)
  • βœ” Initialize attributes inside __init__()
  • βœ” Use self to access instance attributes
  • βœ” Keep methods short and focused
  • βœ” Use class attributes for shared data

Conclusion πŸŽ‰

>>β€œClasses and Objects allow you to model real-world things in code β€” turning ideas into structured programs.” ✨

You now fully understand Classes & Objects in Python! Want the next topic? Try Constructors, Inheritance, Encapsulation, Polymorphism, or Class Methods. Just tell me! 😊