Python Modules
Here we will be discussing about the highest level program organization unit, which helps programmers to pack and organize program code and data for reusability purpose.
What is Modular Programming?
Module provides a simple way to organize program components into a system; grouping similar forms of code into a module makes code easier to understand. It is also referred to as program object that binds data and can be used as reference. In other words, it is file that contains Python program. Programmers use the ‘import’ statement to get access to the names in a module’s global scope. We will learn about it a bit later. Python’s module ultimately allows programmers to link individual files to a larger program system.
Roles Of Python Module
import Statement
Programmers can use any Python source code (i.e. .py files) as a module by implementing an ‘import’ statement in another Python program or Python file.
Let us take a scenario, a file name ‘g.py’ be chosen as the top-level file, it will be simple text file of statements & will be executed from top to bottom as the interpreters do. The modules are – ‘a.py’ and ‘b.py’. So to use the codes of a.py and b.py in g.py program, we have to first define the codes as functions in both modules & then import the two Python codes using ‘import’ statement.
The code will be:
#!/usr/bin/python
def karl(text) #a.py File
print(text, 'karl')
Now, the programmer wants to use the codes of a.py into g.py file.
So the program will be:
So the program will be:
Example:
#!/usr/bin/python
import a #g.py file
a.karl ('myself') #Prints "myself karl"
The figure below shows its working:

So the syntax for import statement is:
Example:
#!/usr/bin/python
import module_name1 [, module_name2 [module_nameN]]
The module can be imported only when the interpreter encounters an import statement & if the module is present in the search path, i.e. the directories that the interpreter searches before importing module.
Standard Library Modules In Python
Python comes with a huge collection of regular used utility modules called standard library; which contains a collection of over two-hundred modules along with platform-independent support system for common programming tasks such as network & internet scripting, text pattern matching, object persistence, GUI (Graphical User Interface) programs and much more.
Though these tools are not the part of Python programming language, but programmers can use them by importing the appropriate modules based on the needs. Since they are standard library modules, therefore they will be available & will work portably on most platforms on which the Python will be run.
How import Works?
Because ‘import'(s) are the most important part of Python program structure, let’s go to the inside of its working technique in brief. From the C-programmer’s view point – import operation is like #include of C Language but in reality ‘import’ is not just textual insertion of one file into another. These are runtime operational statements that perform three distinct steps. These are:
It has to be kept in mind, that the above three steps are performed only the first time the module is imported during a program execution. If the program is run later importing the previous modules, bypass all these three steps & simply fetch the already loaded module-object in memory.
Comments
Post a Comment