Wednesday, 9 April 2014

Modules in python


You can use a module to organize a number of Python definitions in a single file. A
definition can be a function, a class, or a variable containing any Python object. Here is
an example:

# python_101_module_simple.py
"""
This simple module contains definitions of a class and several
functions.
"""
LABEL = '===== Testing a simple module ====='
class Person:
"""Sample of a simple class definition.
"""
def __init__(self, name, description):
self.name = name
self.description = description
def show(self):
print 'Person name:
%s description: %s' % (self.name,
self.description)
def test(msg, count):
"""A sample of a simple function.
"""
for idx in range(count):
print '%s %d' % (msg, idx)
def testDefaultArgs(arg1='default1', arg2='default2'):
"""A function with default arguments.
"""
print 'arg1:', arg1
print 'arg2:', arg2
def testArgLists(*args, **kwargs):
"""
A function which references the argument list and keyword
arguments.
"""
print 'args:', args
print 'kwargs:', kwargs
def main():
"""
A test harness for this module.
"""
print LABEL
person = Person('Herman', 'A cute guy')
person.show()
print '=' * 30
test('Test #', 4)
print '=' * 30
testDefaultArgs('Explicit value')
print '=' * 30
testArgLists('aaa', 'bbb', arg1='ccc', arg2='ddd')
if __name__ == '__main__':
main()
 

Running the above produces the following output:
===== Testing a simple module =====
Person name:
Herman description: A cute guy
==============================
Test # 0
Test # 1
Test # 2
Test # 3
==============================
arg1: Explicit value
arg2: default2
==============================
args: ('aaa', 'bbb')
kwargs: {'arg1': 'ccc', 'arg2': 'ddd'}

 

Comments:
The string definitions at the beginning of each of the module, class definitions, and
function definitions serve as documentation for these items. You can show this
documentation with the following from the commandline:
$ pydoc python_101_module_simple
Or this, from the Python interactive prompt:
>>> import python_101_module_simple
>>> help(python_101_module_simple)
It is common and it is a good practice to include a test harness for the module at the end
of the source file. Note that the test:
if __name__ == '__main__':
will be true only when the file is run (e.g. from the commandline
with something like:
"$ python python_101_module_simple.py
but not when the module is imported.
Remember that the code in a module is only evaluated the first time it is imported in a
program. So, for example, change the value of a global variable in a module might cause
behavior that users of the module might not expect.
Constants, on the other hand, are safe. A constant, in Python, is a variable whose value is
initialized but not changed. An example is LABEL, above.


                                        "News powered by"
                                                     
                                                       



No comments:

Post a Comment

About Me

Popular Posts

Designed By Seo Blogger Templates