Python OOP #3

In a moment, we will examine these useful tools called ‘classroom’ in depth. But if you want, let’s go through the last codes we have given before continuing to explain.

Remember, in the last section, we said that the Python codes we wrote can be used both as an independent program and as a module at the same time.

For example, assuming we save the above codes in a file called, we can run this program with a code like python from the command line. When we run this program on the command line or double-click on it, we run these codes as an independent program. Let’s also examine how to import these codes as a module.


Now, let’s start the Python command line under the directory where is located, and import the counter module by giving the following command there:

>>> import counter

As soon as we give this command, program will start running. However, this is not what we want. We do not want to start running. What we want is to use this file as a module rather than as a standalone program and thus access the attributes and functions within this module. At this point, a question comes to mind:

Why would a person want to import a program as a module?

There are several reasons why you might want to access a Python file as a module. For example, you are writing a program and your goal is to test whether the code you wrote works properly. For this, you can transfer your program as a module to the interactive shell environment and run the parts of this module you want to test one by one. Likewise, you may want to take advantage of the functionality within a program you have written or written by someone else in another program. For this purpose, you can use the functions in the related module by calling the relevant program from another program, that is, by transferring that program as a module into another program.

Let’s say we want to check whether the codes in the file called that we wrote above are working properly. For this, let’s make the following change in the codes in the file:

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


Backend Developer, Öğrenci, Blogger

You may also like...

Yorum yap

%d blogcu bunu beğendi: