Logo         Community
The Company
Your Account
Contact Us
Python 101 (part 7): Dinner With A Hungry Giant
Learn to logically group functions together into modules

| Mercury Rising |

Like Alice in Wonderland, I'll start at the beginning - what's a module anyway?

Modules are a way to group related pieces of code together. They allow developers to create a logical container for variables and functions, such that these variables and functions can be used by other programs that require them.

The goal? Very simple: by making it possible to share code in this manner, Python immediately makes it easier to create reusable software, cutting down development and testing time. Take it one step further: by allowing developers to create modules and providing the underpinnings to import them into other programs, Python ensures that a single copy of a module is in use across a system. This simplifies code maintenance by restricting updates and upgrades to a single file.

In the content of Python programming, a module is essentially a text file, ending in a .py extension and containing executable program code. The name of the file is treated as the name of the module; once the module has been imported into a Python program, this name is used in all subsequent references to the module.

Let's illustrate how this works by creating a simple module. Pop open your favourite text editor, and create a new text file containing the following function:

# define a function
def tempConv(temperature, scale):
if (scale == "c"):
fahrenheit = (temperature * 1.8) + 32
return fahrenheit
elif (scale == "f"):
celsius = (temperature - 32) / 1.8
return celsius
return "Cannot convert!"

Save this file as "temperature.py"

Notice that the module does not require the name of the interpreter to be specified as the first line of the script, as do regular Python scripts.

You can now pull this module into any Python program - I'll illustrate how with the command-line interpreter.

Python 1.5.2 (#1, Aug 25 2000, 09:33:37)  [GCC 2.96 20000731 (experimental)] on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import temperature
>>> temperature.tempConv(98.6, "f")
>>> temperature.tempConv(37, "c")
>>> temperature.tempConv(37, "d")
'Cannot convert!'

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback