r/learnpython • • 19d ago

Trying to understand Class, Object, Instance, and self in Python. Is my understanding correct?

I'm currently learning OOP in Python and wrote down some questions regarding object and instance for following code

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

car1 = Car("Red")```

 1) what is class ?

ans :-  a class is an blueprint for thing i want to make.

2) what is object ?

 ans:-  an object is a container that contains properties and behaviours like car1 is object saying that car should be red color

3) what are instances ?

 ans:-  an instance is refers to that specific object in this case car1 is object and also an instance
when we pass self parameter self become car1 cause self points towards that instance
50 Upvotes

33 comments sorted by

View all comments

3

u/StrayFeral 18d ago • Bravo

The class is the blueprint of which we can instantiate many objects. The class could have parent classes. The class could have child classes (subclasses). A class represents only a design. This is why the class is not alive. The class have two things very generally speaking - methods and properties. The properties are just variables which belong to the class. Methods are just functions which belong to the class.

Example:

We could have a class Vehicle. This is very generic. A vehicles can have properties. For example a vehicle can have a color, wheels which could vary by number but are usually 4. A vehicle could move forward, backwards, the wheels could turn left and right. The vehicle could stop (if moving).

class Vehicle:
    def init(self):
        self.color = "red"
        self._gear = "P"  # vehicle is parked
        self.speed = 0  # mph/kmh
        self.odometer = 0  # whoa, a brand new vehicle
        self._engine_started = False  # vehicle is parked
    
    def change_gear(self, new_gear):
        self._gear = new_gear
    
    def start_the_engine(self):
        self._engine_started = True
    
    def stop_the_engine(self):
        self._engine_started = False
    
    def accelerate(self):
        pass
    
    def brake(self):
        pass

Please note - some methods have only the keyword "pass" - this means we don't know yet what the method would do and we would code it later.

Then we could have class Car(Vehicle). The car itself is a vehicle - this is true. But Car(Vehicle) tells Python that the car is a type of vehicle. Then we can do a class Truck(Vehicle). Also class Motorcycle(Vehicle). Why not a class Van(Vehicle). Because the vans and cars and trucks are all vehicles, but the van is not a car, neither a truck - they are different vehicles, still all are vehicles.

Than we could have a class FamilyCar(Car), also class SportsCar(Car), class Dragster(Car) because these are all type of cars. However very generally said the car is also a vehicle. So all cars - FamilyCar, SportsCar and Dragster are vehicles too - they are also children of class Vehicle.

For this reason, despite we declared FamilyCar as a child of Car, even if we don't type a single line of code, FamilyCar automatically would have method brake() and method accelerate() and everything else from Vehicle.

If you understand what i just described, you understand what is a class, but also very important - what is "inheritance".

Instantiation is the process of making a class alive. By instantiating we create objects.

Object is an instance of a class. Objects are alive.

So if we do

my_car = Van
his_car = SportsCar
her_car = FamilyCar

my_car.start_the_engine()
my_car.change_gear("1")
my_car.accelerate()  # off we go!
my_car.brake()
my_car.change_gear("P")  # we're parking
my_car.stop_the_engine()

but if we do nothing for some time Python would throw these into the garbage, so the garbage collector would come to get these (this is like a garbage truck for objects). Also at the end of a Python program, the garbage collector would come for all objects. Because the memory needs to be freed.

Also in this example my_car, his_car and her_car are all objects and are all instances of classes.

End of story.

The class is just a dead blueprint. The object is a live instance of the class. Classes could have parents and could have child classes and this is called inheritance. All children inherit all properties and methods of the parents.

1

u/StrayFeral 18d ago

Important is not to thing is specific implementation. Important is to start abstract. So you start with a car. But the car is a very specific implementation of a vehicle. So I started with a vehicle, declared some other vehicles, like vans and trucks and then declared a car and what cars could exist.