Why Object-oriented Programming (OOP)
I’m sure you have in mind why object-oriented programming? Do I have to know object-oriented programming? There are such questions. Then let’s answer.
You don’t have to know object-oriented programming. Especially in Python, you can do very good things without knowing object-oriented programming. Python does not force you to use object-oriented programming.
Well then you will say, why am I learning object-oriented programming? Now that I have told you why you should use object-oriented programming, then you will see that knowing object-oriented programming is a necessity. Let’s not get discouraged right away. Because object-oriented programming in Python is very simple, especially when you compare it with other programming languages. If you are learning object-oriented programming for the first time, you are very lucky to learn with Python. Then why to the question of object-oriented programming? Let’s answer.
- You must learn to read code written in object-oriented programming and understand what it is.
- You will have to use it when you have to divide things up when you are faced with very large projects.
- Finally, if you want to be a good software developer, you definitely have to know. Because writing readable and efficient code is just as important as writing code.
In this way, the codes you write with object-oriented programming;
- Readable codes
- Dividing into modularity threads
- Ease of changing codes
- Easier development and extension of the project
- Codes are much easier to maintain
- Ease of adding and reusing the codes in another project because you have divided them into modules.
What is Object-oriented Programming?
Object-oriented programming is a software development method. Object-oriented programming is the real-life version of software. How Does? When we look around, there are many objects around us. There is no need to go far, the one you are using while reading this article; computer, phone or tablet is an object. These objects have some properties and functions. For example, the color, processor speed and brand of your computer, some features of the computer. At the same time, the computer’s power button, keys, etc. are functions that allow this computer to perform some work.
Here we will create objects like computer object. We will give some properties to these objects. At the same time, we will make some operations on these objects. Now let’s compare the objects in real life with the objects we create in software.
We said that an object has properties, but what are the properties of the objects we create? Data types, operators, etc. of the object we created. will be properties of our object. So how do we create their functions? The functions of the object are; We will create it with the help of functions or methods. Ok, we learned what a beautiful object is, so how do we create the objects? So let’s get a headline.
class
Yes, we will create our objects with the help of classes. So what is class
? We have seen some data types in our previous blog posts. For example lists
, tuples
, dictionaries
etc. Like these data types in classes, Python is also a data type.
Classes are used to define groups or sets with common properties. For example, all cars have a brand, model, price, color, etc.
Let’s look at the data types we learned earlier;
print(type(list))
print(type(str))
print(type(tuple))
print(type(int))
print(type(dict))
<class 'type'>
<class 'type'>
<class 'type'>
<class 'type'>
<class 'type'>
It showed us that these data types you see are a class type. So let’s create a class right away.
For this, we will use the class
keyword in Python. And this is our general class creation outline.
class class_name():
...
or
class class_name:
...
You can create it in two different ways, but you will see that we have to use parentheses for inheritance, which we will deal with later.
But this construct is usually used when inheritance is not used;
class class_name:
...
We learned how to create a class, now let’s create a class, for this I simply create a Car class.
class Car:
pass
We created our class, because it does not have a property or function for now, I showed it to be empty with the pass
keyword.
So let’s give some properties to this class
. Let’s give a title for this.
class
Attributes
For example colour, brand, model etc. are attributes of a class.
class Car:
brand = ""
colour = ""
plate = ""
We have created our class
. So how do we create an object? We said at the beginning of the blog that we would create objects with the help of classes. That’s when we created it. Let’s derive (create) an object from the class.
class Car:
brand = ""
colour = ""
plate = ""
# We derive an object from the class
car1 = Car()
car1.brand = "BMW"
car1.colour = "Black"
car1.plate = "B 149 BRN"
Yes, as you can see, we have created an object from a class and assigned the properties of this object.
What is the difference between class
and object
?
Be very careful here. The Car()
class we created is a generic concept. Let’s put it this way, all cars have a brand, model or color. So we make a general classification. But you don’t know the color, make or model of this car. Because the Car()
class is an abstract concept. That’s why you can’t know the properties of an abstract concept. But if you specify properties such as color white, model BMW, you create an object that instantiates that class.
Now let’s create another object
from the class
we created.
class Car:
brand = ""
colour = ""
plate = ""
car1 = Car()
car1.brand = "BMW"
car1.colour = "Black"
car1.plate = "B 149 BRN"
car2 = Car()
car2.brand = "Mercedes-Benz"
car2.colour = "White"
car2.plate = "B 150 BRN"
print("-------CAR 1------")
print("Brand: {} \nColour: {}\nPlate: {}".format(car1.brand, car1.colour, car1.plate))
print("-------CAR 2------")
print("Brand: {} \nColour: {}\nPlate: {}".format(car2.brand, car2.colour, car2.plate))
Let’s see the output:
-------CAR 1------
Brand: BMW
Colour: Black
Plate: B 149 BRN
-------CAR 2------
Brand: Mercedes-Benz
Colour: White
Plate: B 150 BRN
As you can see, friends, you can create multiple objects
from a class
. This is the purpose of object-oriented programming, write once, use many times.
We created the class
and gave it some properties. At the beginning of the blog, when we were explaining the concept of object
, we said that it has certain properties and functions. Now let’s give some functions to our classes, for example; acceleration
, running
, etc. are some functions of that object.
Then let’s add speed functions to our car.
def increaseSpeed(self):
self.speed +=10
return self.speed
We created the speed function of the object. Here is the self
keyword that we have never mentioned before.
self
So what does this keyword do? This keyword refers to the function or property that we created. You can think of it this way, when you try to access this function or feature from the outside, you can reach it thanks to this keyword.
And the first parameter of every function you create inside the class must be the keyword self
. Now let’s call the function we created.
car.increaseSpeed()
class Car:
brand = ""
colour = ""
plate = ""
speed = 0
def increaseSpeed(self):
self.speed += 10
return self.speed
car = Car()
car.brand = "BMW"
car.colour = "Black"
car.plate = "B 149 BRN"
print("------- CAR ------")
print("Brand: {} \nColour: {}\nPlate: {}".format(car.brand, car.colour, car.plate))
car.increaseSpeed()
print("Speed: ", car.speed)
Output:
------- CAR ------
Brand: BMW
Colour: Black
Plate: B 149 BRN
Speed: 10
The more we call the function, the more we run the function.
car.increaseSpeed()
car.increaseSpeed()
car.increaseSpeed()
car.increaseSpeed()
car.increaseSpeed()
Output:
------- CAR ------
Brand: BMW
Colour: Black
Plate: B 149 BRN
Speed: 50
Very nice, now we can create our own object
, we can give properties and functions to our objects. Now let’s organize our objects a little more. For example, we later gave properties to objects. Can we give properties while creating our object
? Let’s look at it;
class Car:
brand = "BMW"
colour = "Black"
plate = "B 149 BRN"
speed = 30
car = Car()
print("------- CAR ------")
print("Brand: {} \nColour: {}\nPlate: {}".format(car.brand, car.colour, car.plate))
Output:
------- CAR ------
Brand: BMW
Colour: Black
Plate: B 149 BRN
As you can see, when we define an object
, that is, when we create it, we can give it properties. We have only one problem. Let’s show it now;
class Car:
brand = "BMW"
colour = "Black"
plate = "B 149 BRN"
speed = 30
car = Car()
car2 = Car()
print("------- CAR - 1 ------")
print("Brand: {} \nColour: {}\nPlate: {}".format(car.brand, car.colour, car.plate))
print("------- CAR - 2 ------")
print("Brand: {} \nColour: {}\nPlate: {}".format(car2.brand, car2.colour, car2.plate))
Let’s look at the output;
------- CAR - 1 ------
Brand: BMW
Colour: Black
Plate: B 149 BRN
------- CAR - 2 ------
Brand: BMW
Colour: Black
Plate: B 149 BRN
As you can see, both objects have the same properties, so how can we create different types of objects? Let’s have a look.
Python __init__()
Function
So what is this function? This function is the Python constructor function. What is a constructor function? The constructor function is the function that is run by default when the Python object
is created. While we were telling you about the self
keyword just a moment ago, we made a definition as follows: βThis keyword refers to the function or property we created, as a reference. Likewise, you can keep the __init__()
function in mind as Python’s reference keyword to keep it in mind.
Let’s show you with an example, then you will understand much better.
class Car:
def __init__(self):
print("The constructor function __init__() ran...")
Car = Car()
Let’s look at the output;
The constructor function __init__() ran...
As you can see we did not call the function. If you remember, we created the increaseSpeed()
function. and to call the function we did it like this:
car.increaseSpeed()
But we didn’t do it for the __init__()
function because when we defined it, we said the function is automatically executed. That’s why this function worked automatically when we ran the program.
Now let’s use the __init__()
function;
class Car:
def __init__(self, brand, colour, plate, speed):
self.brand = brand
self.colour = colour
self.plate = plate
self.speed = speed
car1 = Car("BMW", "Black", "B 149 BRN", 260)
car2 = Car("Mercedes-Benz", "White", "B 150 BRN", 100)
print("------ Car - 1 ------")
print(car1.brand)
print(car1.colour)
print(car1.plate)
print(car1.speed)
print()
print()
print("----- Car - 2 -------")
print(car2.brand)
print(car2.colour)
print(car2.plate)
print(car2.speed)
Let’s look at the output;
------ Car - 1 ------
BMW
Black
B 149 BRN
260
----- Car - 2 -------
Mercedes-Benz
White
B 150 BRN
100
It’s good, but can we set a default value here? Let me show you with an example;
class Car:
def __init__(self, brand="BMW", colour="Black", plate="B 149 BRN", speed=260):
self.brand = brand
self.colour = colour
self.plate = plate
self.speed = speed
car1 = Car()
print("------ Car - 1 ------")
print(car1.brand)
print(car1.colour)
print(car1.plate)
print(car1.speed)
Output:
------ Car - 1 ------
BMW
Black
B 149 BRN
260
Well, if you say that when we create a second object, it will have the same properties. Let’s give an example:
class Car:
def __init__(self, brand="BMW", colour="Black", plate="B 149 BRN", speed=260):
self.brand = brand
self.colour = colour
self.plate = plate
self.speed = speed
car1 = Car()
car2 = Car("Mercedes-Benz", "White", "B 150 BRN", 100)
print("------ Car - 1 ------")
print(car1.brand)
print(car1.colour)
print(car1.plate)
print(car1.speed)
print()
print()
print("----- Car - 2 -------")
print(car2.brand)
print(car2.colour)
print(car2.plate)
print(car2.speed)
Output:
------ Car - 1 ------
BMW
Black
B 149 BRN
260
----- Car - 2 -------
Mercedes-Benz
White
B 150 BRN
100
As you can see, the two objects are different.
We can set the default value. Well, if you say I don’t want to give default value to all of them, I want to give default value to the value I want, then let’s show an example.
class Car:
def __init__(self, brand="BMW", colour=None, plate="B 149 BRN", speed=260):
self.brand = brand
self.colour = colour
self.plate = plate
self.speed = speed
car1 = Car(colour="White")
car2 = Car("Mercedes-Benz", "White", "B 150 BRN", 100)
print("------ Car - 1 ------")
print(car1.brand)
print(car1.colour)
print(car1.plate)
print(car1.speed)
Output:
------ Car - 1 ------
BMW
White
B 149 BRN
260
As you can see, we give the key value None
for the properties that we do not want to give the default value. Here we say that we give a null value with None
and we will add a value later.