Module in Python programming language | applied electronics engineering


Module in Python programming language

By Applied Electronics - Sunday, May 22, 2016 No Comments
Module are the highest level program units in Python programming language. Modules are files with .py extension used to store regularly used functions, classes and objects. Other words for modules is package because modules files are just package of functions and data in a single file. They are useful in segmenting and organizing large programs.

An example of a module called is as follows:

Module in Python programming language
"""some of my programs"""

def square(x):
    """calculates the square of given input number"""    return x**2

def myprinter(x):
    """prints out input"""    print(x)

 Once a module is created, the functions and attributes declared inside a module in python can be imported into another module or program using the import method or from method.

import myprog

After myprog module has been imported, we can then use the function or method as follows.

>>> 25 

 Alternatively, we can use the from keyword to import specific method directly.

from myprog import myprinter

Then use the method myprinter declared in the module

myprinter('my name is john')
>>> my name is john 

Python module allows code reuse and sharing functions and data across the programs. Consider that we have made a new module called Then we can import the previous module methods into the

import myprog

"""Example module to import other module methods"""
x = int(input("enter a number: "))

y = myprog.square(x)


As you can see at the top of the module, we have imported the myprog module. Then we used the square method defined in the myprog in the newmodule module.

In case you are working with the python console, you will get the following.

>>> import newmodule
enter a number:  2

reload( )

If you are working on python console and if you did change any statement of function in the module that you are currently working with, the change will not be reflected in the console window. Python continues to use the previously defined functions. In order to use the newly changed method, you have to use the reload( ) method to reload the module.

Let's say you made a change in the square function defined in myprog module:

def square(x):
    """calculates the square of given input number"""    return x+2

If you use the square method without reloading the module myprog then it will still compute the square of x.

To reload, you have to import the reload( ) method defined in the imp library and use then the square method.

>>> import imp
>>> imp.reload(myprog)
>>> imp.reload(myprog)
<module 'myprog' from 'D:\\python\\tuts2\\'>
>>> myprog.square(5)

Standard Library Modules

Python programming language is build upon modules. Python contains build in modules which contains methods that supports common programming task related to operating system, text processing, input/output, networking etc. For example, the os and sys are standard library module.

No Comment to " Module in Python programming language "