Mastering Object-Oriented Programming in Python: A Beginner's Guide!
Learn the Fundamentals of OOP in Python with Practical Examples and Exercises
Introduction
This is a comprehensive tutorial for Python beginners on object-oriented programming, the objective of this tutorial is to give you an insight into object-oriented programming and help you understand its basics through practical examples.
In this tutorial, we will cover the following concepts :
What is OOP?
Class vs Object.
Attributes vs Methods.
Inheritance
If you like this article, don't forget to follow me to receive more articles about programming! If you have any questions, feel free to post them in the comments or you can contact me personally.
Requirements
Basic python understanding.
A text editor and python installed.
What Is Object-Oriented Programming in Python?
Object-oriented programming is an approach to modeling real-world elements, such as players in a game or employees in a company. A simple example of an object is a car with properties such as color, speed, brand and weight, and methods such as increase or decrease speed, stop, turn right or left, reverse...
An object has basically two characteristics:
attributes
behavior
Before continuing reading this article ... If you don't know me, my name is Hamza, I am a C# and ML software engineer. If you want to start developing software and learn ML, as a software engineer, I will guide you in this adventure and give you tips every day, follow me and subscribe to my newsletter to receive my daily articles.
Class and Object
We can define a class as a blueprint for the object. We can think of it as a sketch of a car containing all the details about the car. When class is defined, only the description for the object is defined. Therefore, no memory or storage is allocated. Meanwhile, an object (instance) is an instantiation of a class, an object is a car with red color for example and 300km/h as maximum speed.
Here I have created a simple visualization of the difference between a class and an object:
If the concept of OOP is not yet clear in your mind, don't worry! The following practical example will make it easier for you.
OOP In python
Attributes
The attributes are a characteristic of an object.
Let's take the previous example and define a class in python called Car, to define a class in python we use the class keyword as follows:
class Car:
pass
Now let's define our class with some attributes :
class Car:
def __init__(self, color, brand, maxSpeed):
self.color = color
self.brand = brand
self.maxSpeed = maxSpeed
The init function the initializer method and it is called when an object is instantiated, for example if we want to create two cars, each with specific properties, we will create two objects of the previous class, one called car1 and the other car2 for example :
car1 = Car('red', 'audi', 300)
car2 = Car("blue", "BMW", 120)
Now, if we want to get an attribute of an object, we will simply type :
print(car1.color)
then, we will get red as output.
Methods
Methods are functions defined inside the body of a class. They are used to define the behaviors of an object.
Now, let's 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
This function takes two parameters, self which points to the instantiated object, and newMaxSpeed the new maximum speed value.
- Note :
A method must have an extra first parameter in the method definition usually called "self". We do not give a value for this parameter when we call the method, Python provides it If we have a method that takes no arguments, then we still have to have one argument. This is similar to this pointer in C++ and this reference in Java.
Now let's instantiate a car with a maximum speed of 200 and modify it with our method :
car1 = Car('red', 'audi', 200)
car1.changeMaxSpeed(300)
Here, car1 plays the role of self, so it is not necessary to specify it, if you specify it, it will give you an error saying that it was expecting one parameter but two were given.
Inheritance
To understand the concept of inheritance, we will take a simple example, Imagine we have a class called vehicle and our previous class called car, when we first 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 we will create the class Car, we want our class to inherit from the previous class, meaning that we dont want to define the previous attributes and methods again.
class Car(Vehicle):
def __init__(self, color, maxSpeed, brand):
self.brand = brand
Now, we can access the variables and methods of the parent class
So basically, Inheritance is the capability of one class to derive or inherit the properties from another class. The class that derives properties is called the child class and the class from which the properties are being derived is called the parent class.
Thank you for reading, and let's connect!
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Twitter
If you like this article! Don't miss the upcoming ones, follow me and subscribe to my newsletter to receive more!
See you soon :)