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
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) # AudiNote
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 Attribute | Instance Attribute |
|---|---|
| Shared by all objects | Unique to each object |
| Defined outside methods | Defined 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) # 7009. 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
selfto access instance attributes - β Keep methods short and focused
- β Use class attributes for shared data
Conclusion π
You now fully understand Classes & Objects in Python! Want the next topic? Try Constructors, Inheritance, Encapsulation, Polymorphism, or Class Methods. Just tell me! π