Interview question
What is the difference between a class and an object? Class और object में क्या अंतर है?
Answer
| Aspect | Class | Object |
|---|---|---|
| Definition | Blueprint/template | Instance of class |
| Type | Logical entity | Physical entity |
| Created | Once during coding | Multiple times at runtime |
| Memory | No memory allocated | Memory allocated when created |
| Example | class Car | car1 = Car() |
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def drive(self):
return f'{self.brand} {self.model} is driving'
# Class is template, object is instance
car1 = Car('Toyota', 'Camry') # Object 1
car2 = Car('Honda', 'Civic') # Object 2
print(car1.drive()) # Toyota Camry is driving
print(car2.drive()) # Honda Civic is driving
# Check type
print(type(car1)) # <class '__main__.Car'>
print(isinstance(car1, Car)) # True| Aspect | Class | Object |
|---|---|---|
| Definition | Blueprint | Instance |
| Type | Logical | Physical |
| Created | Once | Multiple times |
Was this answer clear?