Simple Python Module
2002-09-29 17:52:06
Category: python:modules
Description: Example on creating custom modules with python.
Author: mind
Viewed: 3303
Rating: (9 votes)


#!/usr/local/bin/python
#
# Custom Module example by mind@metalshell.com
#
# an example on creating your own modules and
# importing them into other scripts
#
# 09/25/2002
#
# http://www.metalshell.com
#
 
# to make your own module you need to create a new file: test.py
# and put any custom functions you want in it, listed below is an example:
 
# make a function that will add x to y
def add(x, y): return x + y
 
# make a function that will subtract x from y
def sub(x, y): return x - y
 
# make a function that will divide x by y
def div(x, y): return x / y
 
# make a function that will times x by y
def tim(x, y): return x * y
 
 
# in a seperate file that you want to include the above into you need
# to open the file and add the following:
 
# import's our custom module, note: you don't have to name
# your module file test.py it can be named anything, but when
# you change the name to something else be sure to change the
# below import line to match the module filename..
import test
 
# below is an example of using your custom module
# after you import your module call functions like:
# <module>.function(arguments) note: below I didn't
# add any comments for the simple fact that when this
# example is executed the results speak for themeselves
 
a = test.add(4, 4)
print "test.add(4, 4):", a
 
a = test.sub(4, 8)
print "test.sub(4, 9):", a
 
a = test.div(2, 8)
print "test.div(2, 8):", a
 
a = test.tim(4, 8)
print "test.tim(4, 8):", a