Junior — Middle
What is the main role of a class in the Python programming language?
sobes.tech AI
Answer from AI
The main role of a class in Python is to create user-defined data types that combine data (attributes) and behavior (methods) within a single object. Classes allow modeling real-world entities and organizing code in a convenient and logical structure, supporting object-oriented programming principles such as inheritance, encapsulation, and polymorphism.
Example of a class in Python:
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def drive(self):
print(f"{self.brand} {self.model} is driving")
my_car = Car("Toyota", "Corolla")
my_car.drive() # Output: Toyota Corolla is driving