Modules in Python
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
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:
module_name is the name of the file without
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:
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.
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.
import my_module my_module.doo_hickey('www') my_module.great_printer() print(my_module.A_CONSTANT) print(my_module.name)
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:
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:
import reusing_identifiers reusing_identifiers.great_printer()
second great_printer() definition
Importing Selected Identifiers #
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
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
name2 and so on without prefixing it with the module name.
Here is a program which imports only
name identifier from the module
from my_module import doo_hickey, name doo_hickey('x') print(name)
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 *
* 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.
import module_name will not import names that start with double underscore character.