Sobes.tech
Middle

What is the difference between a class and an object?

sobes.tech AI

Answer from AI

A class is a template or description of an object that defines its properties (attributes) and behavior (methods). An object is a specific instance of a class with actual values.

In simpler terms:

  • A class is like a blueprint of a house.
  • An object is a house built according to that blueprint.

In Python:

class Car:
    def __init__(self, color, model):
        self.color = color
        self.model = model

    def drive(self):
        print(f"The {self.model} car is driving")

# Creating an object (instance of the class)
my_car = Car('red', 'Toyota')
my_car.drive()  # Will output: The Toyota car is driving

Here, Car is the class, and my_car is an object of that class.