Python Module
Python module is a file that contains Python code. Python module's name is the filename without extension. Normally Python module is used to group related functions, classes and variables for better code management and avoiding name clash. For example in the built-in module we have a function called f1 and in our program we also want to have function f1 as well. In this case we need to use module to organize the code so the function has its own namespace.
Writing the first Python module
We are going the write the first module in Python called mymath. In the module mymath, we define a function called circle_area to calculate circle's area. Here is mymath.py looks like:
#!/usr/bin/python
pi = 3.141592
def cicle_area(r):
"""
Calculate circle area
"""
return pi * r * r
The mymath module is faily simple. We define a variable for PI constant and a function to calculate circle's area.
Python import statement
In order to use Python modules in program you need to use he import statement. There are three different forms of import statement as follows:
import modulename
In this form, Python will search for module name with modulename specified followed import keyword and make it avaible in the calling program. If the modulename is not found, an error is issued.
The second form of import module explicitly specify the name will be imported to the program as below:
from modulename import name1, name2, name3...
In this form name1, name2, name3 and ect are imported to the calling program.
The third form of import statement as follows:
from modulename import *
In this form, the * means all public name in the module modulename will be exported to the calling program. Public names are all the names which are not start with underscore (_). If you import two or more modules which have the same name, the name of the last module will replace the name of preceding module in the list.
Using module in the program
Let's start using the mymath module above in our program. In the first example we will use the first form of the import statement. Here is the main program:
#!/usr/bin/python
import mymath
r = 10
area = mymath.circle_area(100)
print('The area of circle is', area)
In the first form of import statement, in order to call function we have to use module name as prefix then the dot notation as above.
If you want to omit the module when you call the function in the importing module, you need to use the second form of import statement.
#!/usr/bin/python
from mymath import circle_area
r = 10
area = circle_area(100)
print('The area of circle is', area)
Private names in Python modules
All the names starting with an underscore will not be exposed to the outside of the module. Those functions, classes and variables are used inside the module only. If you have functions that you don't want other programs use them, you should name them with leading underscore. In addition, this ensure that the third form of the import statement will not bring private names to the calling program.