OverIQ.com

Modules in Python

Last updated on July 27, 2020


Creating modules #

Different languages provides different ways of modularizing code. In Python, we use modules to organize large programs. Python standard library uses modules extensively to organize related functions, classes, variables and constants. We have already used some built-in modules like math, datetime, random; etc a few times in the earlier lessons. You don't have need to organize your programs into modules, however if your program is several pages long or you want to reuse your code, you should definitely use modules.

So what is module anyway ?

A module is an ordinary Python file that ends with .py extension. We can define a group of classes, functions, variables, constants and so on inside a module. To reuse the classes or functions defined inside the module, we have to import the module in our program using the import statement. The syntax of import statement is as follows:

import module_name

where module_name is the name of the file without .py extension

The import statement searches the module, parses and execute it's content and makes it available to the client program. A client program or simply client is a program which uses classes, functions or variables defined in a module without knowing the implementation details. To refer to the module class, function or variables in the client program prefix it with the module's name. For example, to call a function named timer() defined inside a module named great_module, in a client program do this:

great_module.timer()

If import statement failed to find the module, ImportError error will be generated.

Let's take an example:

Create a new file named my_module.py and add the following code to it.

python101/Chapter-14/my_module.py

1
2
3
4
5
6
7
8
9
A_CONSTANT = 100

name = 'a variable'

def great_printer():
    print("first great_printer() definition")

def doo_hickey(arg):
    print("doo_hickey() called with argument:", arg)

Now we create a separate program (or client) named test_module.py in the same directory as my_module.py with the following code.

python101/Chapter-14/test_module.py

1
2
3
4
5
6
import my_module

my_module.doo_hickey('www')
my_module.great_printer()
print(my_module.A_CONSTANT)
print(my_module.name)

Output:

1
2
3
4
doo_hickey() called with argument: www
first great_printer() definition
100
a variable

In a module, identifiers used to name entities like class, function or variables must be unique. If two entities using same name is found then the Python will use the last definition. Create a new file named reusing_identifiers.py and add the following code to it:

python101/Chapter-14/reusing_identifiers.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
A_CONSTANT = 100

name = 'a variable'

def great_printer():
    print("first great_printer() definition")

def doo_hickey(arg):
    print("doo_hickey() called with argument:", arg)

def great_printer():
    print("second great_printer() definition")

The code is exactly same as my_module.py, the only difference is that here we are defining another function named great_printer() at the end of the file.

Now we have two functions using the same name (great_printer). As a result, Python will use the last definition of great_printer() function. Create another client program named test_module2.py with the following code:

python101/Chapter-14/test_module2.py

1
2
3
import reusing_identifiers

reusing_identifiers.great_printer()

Output:

second great_printer() definition

Importing Selected Identifiers #

The statement import my_module imports every identifier in the module to the client program. In some cases, we only want to use some specific names from the module. Let's say from the module my_module.py, we only want to import doo_hickey() function and name variable in our client program. For situations like this, there exists another form of import statement which allows us to import only specific names from the module. Its syntax is:

from module_name import name1, name2, ... nameN

where name1, name2 and so on are names of the entities we want to import in our client program. Any code after this import statement can use name1, name2 and so on without prefixing it with the module name.

Here is a program which imports only doo_hickey and name identifier from the module my_module.

python101/Chapter-14/importing_selected_identifiers.py

1
2
3
4
from my_module import doo_hickey, name

doo_hickey('x')
print(name)

Output:

1
2
doo_hickey() called with argument: x
a variable

In case you want to import every identifier into your client program do this:

from module_name import *

Here * indicates all identifiers. This statement is functionally equivalent to import module_name. The only difference is that the former one allows to access identifiers in a client program without prefixing it with the module name.

Note: import module_name will not import names that start with double underscore character.