
Introduction
To begin with, Python Python is one of the trendy programming languages. It is pretty easy to understand and used to develop advanced applications with evolving circumstances. The demand for knowledge and skills in Python is growing and most companies are looking for skilled Python developers. Most importantly, Python programming is being increasingly used in several industries like web development, machine learning, Internet of Things,Data science with its powerful libraries.
What are Python Modules?
In simple terms, Python Module is a file that consists of python code(executable statements) to perform a specific task.The Python standard library contains well over 200+ modules.Python modules include runnable code that define functions, classes and variables.
The import Statement
Basically, the import statement is used to transfer all of the python code from one file to another Python file.The Python code in one module can be accessed by another module using the importing process. The import statement is the most common way of doing it. Functions such as importlib.import_module() and built-in __import__() can also be used to perform the importing process. Importing is done in the header section of the code before writing any specific functions.We import a module to make the code available in the current program as a separate namespace.
Example
import math math.pow(2,4) The “from import” statementTo import a specific function, Use from … import ….. This method of importing allows us to call the function without using the dot-notation.
Example
from math import cos, pi print(‘cos(pi) is’, cos(pi))When we use import x, it will create a reference to that module in the current namespace of x. So we need to define a module path to access a particular attribute or method from that module. On the other hand, if we uses from x import *, it creates a reference to all public objects in x within the local namespace of the current module. We won’t have to define a module path such as x.name, but can simply refer to it as name.
Points to be noted
- Imports should be written at the top of the file after any comments and docstrings.
- Use blank space to divide the group of imports
- Write your imports alphabetically
The three import groups are
- standard library imports with built-in modules,
- Related third party imports – modules that are installed but not belong to the current application,
- local application imports – modules that belong to the current application
The “dir()” function
The function dir python is responsible for returning the valid list of the attributes for the passed objects in the current local scope. It can have one optional parameter.
dir() syntaxdir(object) object – can be any user-defined object or tuple, list, set, dictionary etc.
Examples
use a single module to hold all the global variables to be used. import the module to use the global variables. If you modify them then it will be visible in other modules
The reload() function
The Python reload() function is used to reload a previously imported or loaded module. In a test environment, when you repeatedly run a test script, reload the modules to make use of the changes made in the code.The argument passed to the reload() must be a module object which was imported before.
- Python module’s code can be recompiled and re-executed with a new set of objects in the module’s dictionary. The init function of the modules can not be loaded again.
- The old objects are reclaimed when their reference count becomes zero.
- The names in the module namespace are changed to new objects if any.
Example
- Python training in Chennai to develop the industrial skill set with assured placement support.