A class is like a blueprint for creating objects.
🔧 Real-life analogy:
A Car class is like the blueprint of a car. It defines what a car should have (like wheels, color) and what it should do (drive, brake).
You can create many cars (objects) from that one blueprint.
class ClassName:
def __init__(self, param1, param2):
self.param1 = param1
self.param2 = param2
def method(self):
# do something
An object is an instance of a class.
🔧 Real-life: If Car is the class, then my_car = Car("Red", 4) is an object (a specific car).
__init__()
ConstructorThe __init__()
method is a special method that runs automatically when you create an object. It's used to initialize object data.
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
my_car = Car("Toyota", "Red")
print(my_car.brand) # Output: Toyota