What is a Module?
We have always avoided code duplication throughout the Python tutorials series. We used functions and classes for this. So can we use these functions or classes in another project? In this lesson, we will cover this subject a little more deeply. Let me define the modules immediately; Modules are Python files that contain functions, classes, and properties. You can call and use these files in any project you want. You are confused, yes, you did not hear wrong, each file you create in Python is actually a module.
There are two kinds of modules in Python;
- Ready modules
- Modules we created ourselves.
In this lesson, we will create our own module. In the next lessons, we will cover ready-made modules. So do we have to use these modules? If we use modules, what advantages and disadvantages do we see?
Why Modules?
Projects that are large and have a lot of developers are generally divided into modules. In this way, it provides us with such advantages in the project that is divided into parts.
- Modules avoid code duplication.
- Modules make our project more readable.
- It is simple to change, update and add new modules to the project in modules.
- It is easy to maintain in the project in modular structure.
- Modules allow more than one person to work on the same project.
Creating a Python Module and Including it in the Project
We already know that the Python page we created is a module, so let's create a new file right away;
This is a Python file called moduls.py
.
We add the function we created as follows.
def printScreen():
print("Hello world!")
Now we create another Python file. We call this file main.py
. We will use the modules we have created here.
Now we call the moduls.py
file we created, that is the module.
import moduls
Now let's take a look at what's inside this module. We use the dir()
function, which we used many times in our previous tutorials.
Our main.py
module is as follows;
import moduls
print(dir(moduls))
Output:
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'printScreen']
In our previous tutorials, we said that functions starting with __
are special functions. Notice that the last element of the list is the printScreen()
function that we created.
So can we use this function? Let's see now.
import moduls
moduls.printScreen()
# Hello world!
As you can see, we used the functions we created successfully. So, let's see how we're going to use classes.
If you are unfamiliar with Python OOP concepts, please refer to this tutorial first.
class Student():
def __init__(self, name, surname, number):
self.name = name
self.surname = surname
self.number = number
def information(self):
print("Name: {}\nSurname: {}\nNumber: {}".format(self.name, self.surname, self.number))
stu = Student("Baransel", "Arslan", 149)
stu.information()
Now let's run the class we created on the main.py
file.
The point we need to pay attention to here is that we are creating an object from the class we created, otherwise we cannot use this class because the Student()
class we created is an abstract concept.
Now let's run the main.py
module.
Name: Baransel
Surname: Arslan
Number: 149
You will notice here is that we created and called a student class object on the moduls.py
page before, so when we called the moduls.py
module, the Student()
class was automatically run. Well, what should we do if we want to call and use the module whenever we want in the project we add.
For this, we create our object on the main.py
page, not on the moduls.py
page.
import moduls
stu = Student("Baransel", "Arslan", 149)
stu.information()
Another calling method is as follows;
import moduls
Student("Baransel", "Arslan", 149).information()
Output:
Name: Baransel
Surname: Arslan
Number: 149
You can use it any way you want. But I strongly recommend that you thoroughly understand Python OOP logic first.
Now let's come to another point to pay attention to. We just used a syntax structure like this when we included a module in the project.
import module_name
This usage is a standard usage. There are other uses as well. Let's see now.
For example, if the module name you are calling is too long and difficult to use, you can add a project as follows.
import module_name as alias
So we gave our module an alias. Let's use it now.
import moduls as mod
mod.Student("Baransel", "Arslan", 149).information()
Let's do another method, for example, we don't want to call all the functions and classes in the module, but if we want to call the functions and classes we need, we do it like this.
from module_name import function, class
Let's use it now.
from moduls import printScreen
Since we are calling the printScreen()
function in moduls.py
here, we can only use this function. We cannot use the Student()
class. If we want to use it, we also add the Student()
class.
from moduls import printScreen,Student
Well, if there are maybe 100 functions or classes in the module, are we going to add them all one by one? Of course not, we already did that.
import moduls
or
from moduls import *
Thus, we can now create our own modules and include them in a project.
Top comments (0)