Python Programming – Modules

In this Page, We are Providing Python Programming – Modules. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – Modules

Modules

A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement; they also run if the file is executed as a script.

Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module. Thus, the author of a module can use global variables in the module without worrying about accidental clashes with a user’s global variables.

Modules can import other modules. It is customary but not required to place all import statements at the beginning of a module. The imported module names are placed in the importing module’s global symbol table.

There is a variant of the import statement that imports specific names from a module directly into the importing module’s symbol table. For example, specific attributes of fibo module are imported in the local namespace as:

>>> from fibo import fib, fib2 
>>> fib ( 500 )
1 1 2 3 5 8 13 21 34 55 89 144 233 377

This does not introduce the module name from which the imports are taken in the local symbol table (so in the example, fibo is not defined).

There is even a variant to import all names that a module defines:

>>> from fibo import *
>>> fib ( 500 )
1 1 2 3 5 8 13 21 34 55 89 144 233 377

This imports all names except those beginning with an underscore ( _ ).

Note that in general, the practice of importing * from a module or package is discouraged, since it often causes poorly readable code. However, it is touse it to save typing in an interactive sessions. For efficiency reasons, each module is only imported once per interpreter session. If changes are made in many modules, it is a wise approach to restart the interpreter or if it is just one module that needs to be tested interactively, use reload (), e.g. reload (modulename).

Executing modules as scripts

When a Python module is run at command prompt

$ python fibo . py <arguments>

the code in the module will be executed, just as if it imported, but with the ___name___ set to ___main___ . That means, by adding the following code at the end of the module:

if __name___ == " ___main___ " :
import sys
fib ( int ( sys . argv [ 1 ] ) )

the file is made usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file:

$ python fibo . py 50 
1 1 2 3 5 8 13 21 34

The Module Search Path

When a module named f ibo is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named fibo.py in a list of directories given by the variable sys. path. The variable sys. the path is initialized from some of these locations:

  • The current directory.
  • PYTHONPATH environment variable (a list of directory names, with the same syntax as the shell variable PATH).

After initialization, Python programs can modify sys .path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory.

Math module

Python has math module and it is always available. The functions provided in this module cannot be used with complex numbers; use the functions of the same name from the cmath module for support of complex numbers. The distinction between functions which support complex numbers and those which does not is made since most users do not want to learn quite as much mathematics as required to understand complex numbers. Except when explicitly noted otherwise, all functions return float values.