ConcepttheModulein python
a wordModuleIt is called a module in Arabic, and it means a regular Python file that contains a set of variables, functions and classes that you can include in your program.
So which file extension .pyisModulein Python.
idea theModule In Python it is to make the code available for reuse, as putting the code in a special file allows you to move it and use it in your programs whenever you need it.
In addition, it helps you organize your projects, for example if you are building a program, a website or a game, etc.. You will find that dividing one project into a groupModulesIt is very necessary because it will be very easy for you to develop and maintain it in the future.
constructionModulein python
to createModuleNew to any project, follow these steps:
Right-click on the name of the project in which you will create the module.
Mouse over the wordNew.
tap onPython File.
Give the file any name like
MyModule.clickOKSo that a Python file with that name is created in the project.
Now, notice that a new module has been created within the same project namedMyModule.py
DeterminetheModuleBasic project in Python
In the project we created a while ago, we notice that the project now contains two files:
Test.pyMyModule.py
Since the project has more than one file, you will always have to pay attention to which file the commands placed in it will be executed when you click the run button.
Find out which module will be executed when the play button is clicked
Simply put, the name of the module that will be executed when you click the play button appears next to it. For example, we notice that the module is still the module Testthat will be executed when the play button is clicked, and the reason is that we did not change it.
Determine which module will be implemented in the project
There are several ways to do this, the easiest way is to click on the drop down menu and choose the module you want to work when you click the play button.
note
In all the examples that we will give in this lesson, we will always execute the commands in the moduleTest.
We will also include the content of the module MyModulein the moduleTest.
Include all contenttheModuleinModulePost in Python
First, we use the reserved word importto include the content of a module in another module.
Note: If you do the importsame module more than once, the Python interpreter will not do importit again.
In the following example, we have defined a module whose name MyModulecontains one function, and another module whose name isTest.
Then we included all the module content MyModulein the moduleTest.
Example
def greeting():
print('Welcome to harmash.com')
# MyModule Here we have included all the mod content import MyModule # MyModule in the greeting() module here we called the function MyModule.greeting()
• We will get the following result when the module is turned onTest.
Whatch out
If you do importall the content of the module, it is as if you are adding a complete copy of it in memory even if it contains 1000 functions or 1000 objects.
So it is always better to do importonly the things you need from the module and not all modules.
Include only the things we need fromtheModulein python
To specify the things we want to use only from the module, we put the reserved word from, then we put the name of the module, then the word import, and then we put the name of the things we want from the module with a comma between each two things.
Note: When including objects from a module we only mention their names in a sentenceThe import.
And when we use the things that were brought from the module, we use them directly without the need to mention the name of the module we brought from it.
In the following example, we have defined a module whose name MyModulecontains two functions and an object of its typedict.
Then we defined another module namedTest.In this module we have included only the things we need from the moduleMyModule.
Example
# Consists of 3 data elements called dict. Here we have defined
data = {
'id': 5,
'name': 'Sara',
'gender': 'female'
}
# Here we have defined a function that we pass a name on when called, and it prints a welcome message for that name.
def login_msg(name):
print("Welcome", name)
# Here we have defined a function that we pass a name on when it is called and it prints a farewell message for that name
def logout_msg(name):
print('Good by', name)
# MyModule only from the login_msg() module and the data function here we have included the object from MyModule import data, login_msg # name in the data variable in the name element here we have stored the value of the key name = data['name'] # name and pass the name we want it to print which we originally stored in the login_msg() variable here we called the function login_msg(name)
• We will get the following result when the module is turned onTest.
Include all contenttheModuleYou can only call him by his name in Python
In the event that you want to include all the content of the module in another module without having to mention the name of the module whenever you want to use it, you can put the keyword fromafter that we put the name of the module, then the keyword importand then we *just put.
In the following example, we have defined a module whose name MyModulecontains two functions and an object of its typedict.
Then we defined another module namedTest.In this module we have included all the things in the moduleMyModule.
Example
# Consists of 3 data elements called dict. Here we have defined
data = {
'id': 5,
'name': 'Sara',
'gender': 'female'
}
# Here we have defined a function that we pass a name on when called, and it prints a welcome message for that name.
def login_msg(name):
print("Welcome", name)
# Here we have defined a function that we pass a name on when it is called and it prints a farewell message for that name
def logout_msg(name):
print('Good by', name)
# MyModule Here we have included everything found in from MyModule import * # name in the data variable in the name element here we have stored the value of the key name = data['name'] # name and pass the name we want it to print which we originally stored in the login_msg() variable here we called the function login_msg(name)
• We will get the following result when the module is turned onTest.
Attention
It is preferable not to use this method because it may cause you many problems that you do not need.
For example, if you invoke more than one module that contains things with the same name, a name collision will occur, in addition to creating an additional burden on memory because of the things you included that you basically don't need.
renametheModuleWhen embedding it in Python
In case you want to give another name(Alias)For the module when you include it to call him instead of calling him by his original name, you can do this with the keywordas.
So, if the module name is long, you can put a short name for it instead of having to type in its full name whenever you want to access its content.
In the following example, we have defined a module whose name MyModulecontains one function, and another module whose name isTest.
Then we included all the content of the module MyModulein the module, MyModulegiving the module a short name.
Example
def greeting():
print('Welcome to alawiri.com')
# As an abbreviation for the 'mod' name with the word MyModule here we have included all the content of the module import MyModule as mod # mod in the module we called greeting() here we called the function mod.greeting()
• We will get the following result when the module is turned onTest.
inclusionModuleReady in Python
In the previous lessons, we previously included the module maththat contains ready-made functions and constants that can be used when performing arithmetic operations, and we explained the most important functions in it. So we will now use another ready-made module calledplatform.
In the following example, we have included a ready-made model named after platformit, and through it we know all the characteristics of the computer we are using.
Example
# platform Here we have included all mod content
import platform
# Here we have displayed the name of the operating system
print('Operating System:', platform.system())
# Here we have displayed all the available information about the processor
print('Processor:', platform.processor())
# Here we have shown the Python version installed on the device
print('Python Version:', platform.python_version())
• You will get a result similar to the following when running according to your computer specifications.
Processor: Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
Python Version: 3.6.4
Extract the names of all the things in theModulein python
To find out all the object names in any module, you can pass the module name as a value dir()to the function and then print what it will return.
In the following example, we have printed the names of all the objects in the modulemath.
Example
# math Here we have included all the content of the module import math # dir() which the math function will return Here we have shown the names of all the objects in the module print(dir(math))
• You will get the following result when running.