Python OOP

Photo by luis gomes on Pexels.com

Object Oriented Programming (OOP)

In this article, we will give an introduction to the object-oriented programming (python oop) approach, which will constitute a significant part of our programming activities, and we will touch on classes, one of the basic concepts of this approach. In this article, our aim will be to get to know both object-oriented programming through classes, to gain basic information about this approach, and to be able to understand most of the object-oriented structures we see around us. After completing this article, we can claim that we have an intermediate knowledge of object oriented programming.

Introduction

The whole purpose of this programming language is based on the idea that “the codes written once can be used repeatedly in the most efficient way”.


Before we get into object-oriented programming, there is one more thing we should mention. If Python is the first programming language you learn, you may find object-oriented programming difficult to learn (although it actually is not), you may find this subject a little complicated. In this case, you inevitably ask yourself the question: Do I have to learn object-oriented programming? The short answer to this question is that if you want to be a good programmer you have to learn object-oriented programming.

Advertisements

Classes in Python OOP


On the basis of object-oriented programming, there is a concept named ‘class’ (class) which we mention in the introduction section above. In this section, we will try to deal with this basic concept properly.

So what exactly is this class thing? Classes are the data types that enable us to produce objects, if we were to define them very roughly and very abstractly. Here, object-oriented programming, as the name suggests, is a programming activity based on objects (and therefore classes).

I can hear you say, “I didn’t understand anything!” Because the above definition does not answer questions such as what does ‘object’ mean, what does ‘class’ mean. In other words, we cannot predict what we use the words ‘object’ and ‘class’ in terms of programming by looking at the definition above. If you also agree, read on…

What Do Classes Do in Python OOP?


So what is the ‘class’ data type that we will discuss in this article? If you wish, let’s try to explain this on a simple example. Let’s say you want to write a code that counts the letters we specify in a word that the user enters.



The simplest code you can write to achieve this goal would probably be:

letters_i_choose = 'aebwxzki' #I specify them and I can type the letter I want.
counter = 0 #This will act as a counter for me.

sentence = input('Enter a sentence: ') #We use it to get input from the user.

for letter in sentence:
    if letter in letters_i_choose:  
       counter += 1   #This part is a cycle and the cycle will continue until the result occurs and move on to the next stage.

message = 'In {} there are/is {} letter I choose'
print(message.format(sentence, counter))

These are very simple codes that work properly. It also fulfills our purpose perfectly. Moreover, since all the items in the codes are in a single namespace / scope, we have no difficulty in accessing them. In other words, we can access letters_i_choose, counter, word, letter, message variables from anywhere within codes.

Don’t worry if you don’t understand what I’m saying about the name / domain. With the example we will give in a moment, you will understand the situation more clearly.

We can take some measures to ease the code structure a little. For example, we can make that part more specific by including the codes that check whether a letter in the word entered by the user is vocal or not, into a function:

letters_i_choose = 'aebwxzki' #I specify them and I can type the letter I want.
counter = 0 #This will act as a counter for me.

sentence = input('Enter a sentence: ') #We use it to get input from the user.

def letterichoose(letter):             #This is a funciton
    return letter in letters_i_choose

for letter in sentence:
    if letter in letters_i_choose:  #This part is a cycle and the cycle will continue until the result occurs and move on to the next stage.
       counter += 1

message = 'In "{}" there are/is {} letter I choose'
print(message.format(sentence, counter))

Here, we have defined a function named letterichoose() that outputs True or False depending on whether the letter we check is in the variable named “letters_i_choose“. If the letter we check is in the variable “letters_i_choose“, the letterichoose() function will output True. Otherwise, we will output False from this function. Thus, wherever we want to check the letters I choose, we will only be able to use the letterichoose() function. This will allow us to use the codes we have written once over and over again.

In the next article, I will make the above codes more general purpose.

An important request

Remember that I can make mistakes, too. The important thing is to be able to correct these errors. Please let me know if you encounter an error or if there is anything you want to improve, via the form below.

Samet Salih
Samet Salih

Junior Software Engineer

Liked it? Take a second to support Selim on Patreon!
Advertisements

Selim

Backend Developer, Öğrenci, Blogger

You may also like...

Yorum yap

%d blogcu bunu beğendi: