python modules

By | March 12, 2019

Python Modules-:  A module is a file containing code. It defines a group of Python functions or other objects and the name of the module is derived from the name of the file.

Modules most often contain Python source code but they can also be compiled C or C++ object files. Compiled modules and Python source modules are used the same way.

In the language without modules, it would be impossible to use two different functions named reverse. In Python the process is trivial. We refer to the functions in our main program as mymodule.reverse and othermodule.reverse.

Using the module names keeps the two reverse functions straight because Python uses namespaces. A namespace is essentially a dictionary of the identifiers available to a block, function, class, module, and so on. 

Modules are also used to make Python itself more manageable. Most standard Python functions are not built into the core of the language but are provided via specific modules which we can load as needed.

We need to load the module in our python code to use its functionality.

  1. The Import Statement
  2. The From import Statement

 

The Import Statement -: The import statement takes three different forms.

import modulename

The most basic is which searches for a Python module of the given name, parses its contents and makes it available. The importing code can use the contents of the module but any references by that code to names within the module must still be pretended with the module name. If the named module is not found an error is generated.

The second form permits specific names from a module to be explicitly imported into the code.

from modulename import name1,name2,name3,.....

Each of name1, name2, and so forth from within modulename is made available to the importing code.Code after the import statement can use any of name1, name2, name3, and so on without your prep ending the module name.

At last  there is a general form of the from . . . import . . . statement as like

from modulename import *

Example of import statement

import file;  
name = input("Enter the name?")  
file.displayMsg(name)

Output of this programme

Enter the name ?
hi netnic

Module Search Path-: Exactly where Python looks for modules is defined in a variable called path which we can access through a module called sys.

>>> import sys
>>> sys.path
_list of directories in the search path_

The value shown in place of _list of directories in the search path_ depends on the configuration of our system. In the string indicates a list of directories that Python searches  when attempting to execute an import statement. The first module found that satisfies the import request is used. If there’s no satisfactory module in the module search path, an ImportError exception is raised.

Private Name Module-:  We can enter from module import * to import almost all names from a module. The exception is that identifiers in the module beginning with an underscore can not be imported with from module import *.

Python Scoping rules and name-spacing -:  A namespace in Python is a mapping from identifiers to objects that is how Python keeps track of what variables and identifiers are active and what they point to. So a statement like x = 1 adds x to a namespace  and associates it with the value 1. When a block of code is executed in Python, it has three namespaces.

  • Local
  • Global
  • Built-in

When an identifier is encountered during execution, Python first looks in the local namespace for it. If the identifier is not found  the global namespace is looked in next. If the identifier still has not been found the built-in namespace is checked. If it doesn’t exist there this situation is considered to be an error, and a NameError exception occurs.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *