Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Python 2.1 Bible    (ISBN: 0764548077)


 

 List Price: $39.99
 Our Price: $27.99
 Used Price: $21.00

 Release Date: June, 2001
 Manufacturer: John Wiley & Sons (Paperback)
 Sales Rank: 32,085

 Author: Dave Brueck, Stephen Tanner









More Info

 Simple Python Module 2002-09-29 17:52:06
  python 
Category: source:python:modules
Description: Example on creating custom modules with python.
Platform: all
Author: mind
Viewed: 3109
Rating: 3.7/5 (9 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
simpmod.py
#!/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
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?