Junior — Middle
Explain what an object is in Python and how it is used.
sobes.tech AI
Answer from AI
In Python, an object is an instance of a class that combines data (attributes) and functions (methods) that work with this data. Objects allow modeling real entities and their behavior.
Example of using an object:
class Car:
def __init__(self, brand, year):
self.brand = brand
self.year = year
def drive(self):
print(f"{self.brand} is driving")
my_car = Car("Toyota", 2020)
my_car.drive() # Output: Toyota is driving
Here, my_car is an object of the class Car, which stores information about the brand and year of manufacture, and the drive method describes the object's action.