r/learnpython • u/Forward-Ride2950 • 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
=
= ```
1) is ?
:- is
2) is ?
:- is and is
3) ?
:- is in is and
pass
50
Upvotes
View all comments
3
u/StrayFeral 18d ago •
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).
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
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.