Python OOP #2

Let’s continue with our previous article for Python OOP and oops concepts in python

If we want to make the codes in the previous article even more general, we can consider including the codes that increment the counter 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

def plus():    #This is a funciton
    global counter
    for letter in sentence:
        if letterichoose(letter):   #This part is a cycle and the cycle will continue until the result occurs and move on to the next stage.
            counter += 1
    return counter

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

If you remember, we said that the biggest advantage (python oop) of the codes we wrote in the first place is that all the elements in the codes are in a single name/domain. In this way, we could access all items from anywhere. In the above codes, there is more than one name/domain:

Advertisements
  • Global name/domain with variables letters_i_choose , counter, letter, and message.
  • The local name/domain of the letterichoose() function.
  • local name/domain of the plus() function.
python oop

This list is actually an example of oops concepts in python

As you know, we can access variables in the global namespace from anywhere. However, we cannot change them everywhere. So for example, we can view the value of the counter variable in the global namespace from the plus() function.

Let’s confirm this:

letters_i_choose = 'aebwxzki'
counter = 0

sentence = input('Enter a sentence: ')

def letterichoose(letter):
    print('The value of the couner veriable is currently: ', counter)
    return letter in letters_i_choose

def plus():
    global counter
    for letter in sentence:
        if letterichoose(letter):
            counter += 1
    return counter

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

As you can see, we were able to use the value of the counter variable in the global namespace within the plus() function. But if we are to make changes to this variable, we have to take additional steps. So, for example, we need to use the global statement to modify the counter variable in the global domain from the domain of the plus() function. In this way, we can increase the value of the variable named counter in the global namespace.

Notice that there are two global variables in the plus() function:
counter and word. But we only set the counter variable globally. For our other global variable, we did not do this for the word. Because we have no intention of changing the variable named word. We are content with just using this variable. So we don’t have to set this variable globally.

However, as you know, using the global statement is not recommended. If you don’t want to use this idiom, you can choose to write the above codes like this:

letters_i_choose = 'aebwxzki'
counter = 0

sentence = input('Enter a sentence: ')

def letterichoose(letter):
    return letter in letters_i_choose

def plus(counter):
    for letter in sentence:
        if letterichoose(letter):
            counter += 1
    return counter

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

As you can see, instead of using the global statement in these codes, we were able to manipulate the counter variable by communicating with the global namespace via the counter parameter we gave to the plus() function. We do not need to do anything special for the global variable word for which we only use its value. By the way, of course, the word we use as the parameter in the plus() function does not have to be the counter.


We could also write our codes like this in Python OOP:

letters_i_choose = 'aebwxzki'
counter = 0

sentence = input('Enter a sentence: ')

def letterichoose(letter):
    return letter in letters_i_choose

def plus(n):
    for letter in sentence:
        if letterichoose(letter):
            n += 1
    return n

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

The important thing is that the plus() function takes a parameter that will allow us to communicate with the global namespace. It doesn’t matter what the name of this parameter is.

By making a few more changes to the above codes, we can make these codes thoroughly extensible:

letter_i_choose = 'aebwxzki'
counter = 0

def ask_sentence():
    return input('Enter a sentence: ')

def letterichoose(letter):
    return letter in letter_i_choose

def plus(counter, sentence):
    for letter in sentence:
        if letterichoose(letter):
            counter += 1
    return counter

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

def run():
    sentence = ask_sentence()
    write_screen(sentence)

run()

In these codes, note how we communicate between items in the local domains of different functions, with the help of the parameters we give to the functions. Since we placed the word variable in the global domain in the previous code into the local domain of the run() function this time, the word variable in the plus() function has become void. So, we gave this variable as a parameter to the plus() function, and when calling this function inside the write_screen() function, we used both the counter and word arguments.

In addition, we have made this part literally “modular”, that is, flexible and removable, by asking the user for words and collecting the code pieces that print the word on the screen, that is, the codes that start our program, under the heading run().

In the next article I will show you how to import the counter function in Python OOP and we will check what is oops concepts in python.

If you need more documents about Python OOP (oop concepts in python) you can check this link

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: