DEV Community

Cover image for Python Object-oriented Programming (OOP)
Baransel
Baransel

Posted on

Python Object-oriented Programming (OOP)

Sign up to my newsletter!.

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 lessons. 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'>
Enter fullscreen mode Exit fullscreen mode

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():
        ...
Enter fullscreen mode Exit fullscreen mode

or

class class_name:
        ...
Enter fullscreen mode Exit fullscreen mode

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:
        ...
Enter fullscreen mode Exit fullscreen mode

We learned how to create a class, now let's create a class, for this I simply create a Car class.

class Car:
    pass
Enter fullscreen mode Exit fullscreen mode

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 = ""
Enter fullscreen mode Exit fullscreen mode

We have created our class. So how do we create an object? We said at the beginning of the lesson 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"
```
{% endraw %}


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 {% raw %}`class` and `object`?
Continue this post on my blog! [Python Object-oriented Programming (OOP)](https://baransel.dev/post/python-object-oriented-programming-oop/).


[Sign up to my newsletter!](https://baransel.dev/newsletter/).
Enter fullscreen mode Exit fullscreen mode

Top comments (0)