In Python programming language, there are different programming styles that can be implemented. Object-Oriented Programming allows a developer to structure their code in a way that mirrors real-world entities and their interactions. A car program that involves different parts of the car and their functions is a practical example. This article would give you an understanding as to how the basic concepts of object-oriented programming works through practical examples.
In this article, I will be talking about the following concepts:
- OOP in Python
- Class vs Object
- Attributes vs Methods
- Inheritance
Requirements
A basic understanding of Python
A code editor and python installed
Understanding Object-Oriented Programming in python
Object-Oriented programming is a programming model that revolves around the concept of creating and using objects to design and build software systems. In simple terms, OOP in Python is like creating recipes (classes) for different things, like animals or cars. These recipes have ingredients (attributes) and instructions (methods). Then, you can use these recipes to make specific things (objects) that follow those instructions and have their own unique ingredients.
Class vs Object
A class is a blueprint or template that defines the structure, attributes, and behaviors of objects. You can think of it as a sketch of a car containing all the details of a car. An object is a representation of a class, an object is a car with green color for example and 320km/h as maximum speed. Here is a simple visualization of the differences between a class and an object:
OOP in python
Attributes
The attributes are the variables that store data associated with an object. They define the properties or characteristics of an object. Looking at the previous example, a class in python called "Car" was defined. To define a class in python, we use the class keyword:
class Car:
pass
Now, let’s define our class with some attributes:
class Car:
def __init__(self, color, brand, maxSpeed):
self.color = color # (Color attribute)
self.brand = brand # (Brand attribute)
self.maxSpeed = maxSpeed # (Max speed attribute)
The "init" function or the constructor is automatically called when an object (instance) of the class is created. It is used to set up the initial state of the object by assigning values to its attributes. Let’s say you want to create two cars with different properties, you would have to create two objects of the class "Car" naming them "car1" and "car2". For example:
car1 = Car("Red", "Toyota", 120)
car2 = Car("Blue", "BMW", 300)
Now to get the attribute of an object, let’s say the color of car1, you type:
print(car1.color)
then you’ll get red as the output.
Methods
Methods are functions that are defined within a class and are associated with objects created from that class. Methods define the behavior that objects of the class can perform. For example, define a method called "changeMaxSpeed()":
class Car:
def __init__(self, color, brand, maxSpeed):
self.color = color
self.brand = brand
self.maxSpeed = maxSpeed
def changeMaxSpeed(self, newMaxSpeed):
self.maxSpeed = newMaxSpeed
Note A method must have an extra first parameter in the method definition usually called "self". You do not give a value for this parameter when you call the method, Python provides it. Even if you have a method that takes no arguments, then you still have to have one argument which is this "self" since it is a required placeholder for the instance.
This function takes two parameters, "self" which points to the instantiated object, and "newMaxSpeed" the new maximum speed value. Now instantiate a car with a maximum speed of 120 and modify it with our method:
car1 = Car(“blue”,“toyota”,120)
car1.changeMaxSpeed(300)
Here, "car1" acts as "self", so it is unnecessary to specify it, if you specify "self", an error would occur saying it was expecting one parameter but got two.
Inheritance
To understand the concept of inheritance, we will take a simple example, Imagine you have an initial class called "vehicle" before the previous class called "car", you created the class "vehicle" as follows :
class Vehicle:
def __init__(self, color, maxSpeed):
self.color = color
self.maxSpeed = maxSpeed
def changeMaxSpeed(self, newMaxSpeed):
self.maxSpeed = newMaxSpeed
def changeColor(self, newColor):
self.color = newColor
Now when you create the class "Car", you want your class to inherit from the initial class "Vehicle", meaning that you don't have to define the previous attributes and methods again.
class Car(Vehicle):
def __init__(self, color, maxSpeed, brand):
self.brand = brand
Now, you can access the variables and methods of the parent class. So basically, Inheritance allows a class to inherit or derive properties from another class. The class that derives properties is called the child class(e.g. Car) and the class from which the properties are being derived is called the parent class(e.g. Vehicle).
Conclusion
So far, we have seen how to approach a real-world problem using object-oriented programming. We have also seen how to create classes and use them to create solutions to problems. You also saw how OOP lets classes share traits promoting efficient code reuse. So, in a nutshell, OOP in Python is about creating organized, reusable code using classes, objects, attributes, methods, and inheritance for smoother and more efficient programming adventures!
Thank you for reading, and let's connect!
Thank you for reading my blog. Feel free to send me a message on twitter , if you have any questions or suggestions. If you like this article! Don't miss the upcoming ones, follow me to read more! Thank you and have a nice day, developer!