modules

=Modules= 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.

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, hello.py code def print_func( par ): print "Hello : ", par return code || = The //import// Statement: = You can use any Python source file as a module by executing an import statement in some other Python source file. //import// has the following syntax: code import module1[, module2[,... moduleN] code || When the interpreter encounters an import statement, it imports the module if the module is present in the search path. Asearch path is a list of directories that the interpreter searches before importing a module.

Example:
To import the module hello.py, you need to put the following command at the top of the script: code #!/usr/bin/python

import hello
 * 1) Import module hello

hello.print_func("Zara")
 * 1) Now you can call defined function that module as follows

code || This would produce following result: code Hello : Zara code || A module is loaded only once, regardless of the number of times it is imported. This prevents the module execution from happening over and over again if multiple imports occur. = The //from...import// Statement = Python's //from// statement lets you import specific attributes from a module into the current namespace:

Syntax:
code from modname import name1[, name2[, ... nameN]] code ||

Example:
For example, to import the function fibonacci from the module fib, use the following statement: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">from fib import fibonacci code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This statement does not import the entire module fib into the current namespace; it just introduces the item fibonacci from the module fib into the global symbol table of the importing module. = The //from...import *// Statement: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">It is also possible to import all names from a module into the current namespace by using the following import statement: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">from modname import * code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This provides an easy way to import all the items from a module into the current namespace; however, this statement should be used sparingly. = Locating Modules: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">When you import a module, the Python interpreter searches for the module in the following sequences: > > > <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The module search path is stored in the system module sys as the **sys.path** variable. The sys.path variable contains the current directory, PYTHONPATH, and the installation-dependent default.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> The current directory.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> If the module isn't found, Python then searches each directory in the shell variable PYTHONPATH.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> If all else fails, Python checks the default path. On UNIX, this default path is normally /usr/local/lib/python/.

The //PYTHONPATH// Variable:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The PYTHONPATH is an environment variable, consisting of a list of directories. The syntax of PYTHONPATH is the same as that of the shell variable PATH. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Here is a typical PYTHONPATH from a Windows system: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">set PYTHONPATH=c:\python20\lib; code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">And here is a typical PYTHONPATH from a UNIX system: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">set PYTHONPATH=/usr/local/lib/python code || = Namespaces and Scoping: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Variables are names (identifiers) that map to objects. A //namespace// is a dictionary of variable names (keys) and their corresponding objects (values). <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">A Python statement can access variables in a //local namespace// and in the //global namespace//. If a local and a global variable have the same name, the local variable shadows the global variable. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Each function has its own local namespace. Class methods follow the same scoping rule as ordinary functions. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Python makes educated guesses on whether variables are local or global. It assumes that any variable assigned a value in a function is local. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Therefore, in order to assign a value to a global variable within a function, you must first use the global statement. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The statement //global VarName// tells Python that VarName is a global variable. Python stops searching the local namespace for the variable. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">For example, we define a variable //Money// in the global namespace. Within the function //Money//, we assign //Money// a value. therefor Python assumes //Money// is a local variable. However, we access the value of the local variable //Money// before setting it, so an UnboundLocalError is the result. Uncommenting the global statement fixes the problem. code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

Money = 2000 def AddMoney: # Uncomment the following line to fix the code: # global Money Money = Money + 1

print Money AddMoney print Money code || = The dir Function: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The dir built-in function returns a sorted list of strings containing the names defined by a module. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The list contains the names of all the modules, variables, and functions that are defined in a module.

Example:
code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

import math
 * 1) Import built-in module math

content = dir(math)

print content; code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Here the special string variable //__name__// is the module's name, and //__file__// is the filename from which the module was loaded. = The //globals// and //locals// Functions: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The //globals// and //locals// functions can be used to return the names in the global and local namespaces depending on the location from where they are called. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">If locals is called from within a function, it will return all the names that can be accessed locally from that function. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">If globals is called from within a function, it will return all the names that can be accessed globally from that function. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The return type of both these functions is dictionary. Therefore, names can be extracted using the keys function. = The //reload// Function: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">When the module is imported into a script, the code in the top-level portion of a module is executed only once. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Therefore, if you want to reexecute the top-level code in a module, you can use the //reload//function. The reload function imports a previously imported module again.

Syntax:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The syntax of the reload function is this: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">reload(module_name) code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Here //module_name// is the name of the module you want to reload and not the string containing the module name. For example to re-load //hello// module, do the following: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">reload(hello) code || = Packages in Python: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">A package is a hierarchical file directory structure that defines a single Python application environment that consists of modules and subpackages and sub-subpackages, and so on.

Example:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Consider a file //Pots.py// available in //Phone// directory. This file has following line of source code: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

def Pots: print "I'm Pots Phone" code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Similar way we have another two files having different functions with the same name as above: <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Now create one more file __init__.py in //Phone// directory : <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">To make all of your functions available when you've imported Phone, you need to put explicit import statements in __init__.py as follows: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">from Pots import Pots from Isdn import Isdn from G3 import G3 code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">After you've added these lines to __init__.py, you have all of these classes available when you've imported the Phone package: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">//Phone/Isdn.py// file having function Isdn
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">//Phone/G3.py// file having function G3
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">Phone/__init__.py

import Phone
 * 1) Now import your Phone Package.

Phone.Pots Phone.Isdn Phone.G3 code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">I'm Pots Phone I'm 3G Phone I'm ISDN Phone code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">**Note:** In the above example, we have taken example of a single functions in each file, but you can keep multiple functions in your files. You can also define different Python classes in those files and then you can create your packages out of those classes.