A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference.
Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code.
The Python code for a module named aname normally resides in a file named aname.py. Here's an example of a simple module, Hello.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 module1[, module2[,... moduleN]
|
The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc) :
Save this code in a file named, about_us.py
|
Save this code in a file named, my_file.py and Import the module named about_us .
|
You can name the module file whatever you like, but it must have the file extension .py (example : Hello.py, about_us.py etc.)
You can create an alias when you import a module, by using the as keyword. Example :
|
There are several built-in modules in Python, which you can import whenever you like.
|
There is a built-in function to list all the function names (or variable names) in a module. The dir() function.
|
Note : The dir() function can be used on all modules, also the ones you create yourself.
It's a special area where you can find special questions and answers for CSE students or IT professionals. Also, In this section, we try to explain a topic in a very deep way.