What is Module
Module is logical organize of reusable Python Code. Module groups the related code for easy to understand and use. As we know almost everything on Python are object, Module is also an object that can be bind and reference. Simply, a module is a file that define functions, classes and variables. A module can also include runnable code.
I will try to elaborate the concept of Python Module using some simple and easy to understand examples.
Example:
The Python code for a module named “aname” normally resides in a file named aname.py. Here’s an example of a simple module, calc.py-
# calc.py def add(a,b): return a+b def sub(a,b): return a-b def mul(a,b): return a*b def div(a,b): return a/b
I wrote four functions in the calc.py file. We can easily use or call the functions from same file that is calc.py. But, what if , we want to use or call them from another file? To do so we have to import the file or module to the new file. Let create a new file name main.py.You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax:
import module
In order to import calc module in main.py we need to write following code at first on it
#main.py import calc.py
now all the global variables, functions, classes and so on inside calc.py are accessible from main.py. We can use the four functions of calc.py as following
import calc calc.add() calc.sub() calc.mul() calc.div()
The from…import Statement
Python’s from statement lets you import specific attributes from a module into the current namespace. The from…import has the following syntax −
module_name import attribute1, attribute2....
For example, to import the function add from the module calc, use the following statement −
from calc import add
Now, let we have two variables x,y and want to put the summation of them into z
the code is following
#main.py from calc import add x=100 y=200 z=add(x,y) return z #output 300
we can also access other attributes of module.Suppose, the add the constant PI=3.1415926 into calc.py
#calc.py def add(a,b): return a+b def sub(a,b): return a-b def mul(a,b): return a*b def div(a,b): return a/b #PI Constant Added PI=3.1415926
we can access the value of PI as following-
import calc pi=calc.PI print pi
The dir( ) Function
The dir() built-in function returns a sorted list of strings containing the names defined by a module.
The list contains the names of all the modules, variables and functions that are defined in a module. Following is a simple example −
import calc content= dir(calc) print content
output:
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'add', 'div', 'mul', 'sub']
Module is one of vital concept of Python language that frequently used during any kind of python development. I tried to explain the concept in brief using simple examples.
Thats all for today. In the next post I’ll try to discuss on Python Package and Name space. Till then Bye.
Hey there are using WordPress for your blog platform?
I’m new to the blog world but I’m trying to get started and set up my own. Do you need any
coding expertise to make your own blog? Any help would be really appreciated!