Python
  Home arrow Python arrow Page 2 - Python 101 (part 7): Dinner With A Hun...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python 101 (part 7): Dinner With A Hungry Giant
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2001-08-07

    Table of Contents:
  • Python 101 (part 7): Dinner With A Hungry Giant
  • Mercury Rising
  • Between A Rock And...Another Rock
  • Love Bytes
  • Enter The Hungry Giant
  • From Python, With Love
  • Doing The Math
  • String Theory (And Other Interesting Stuff)
  • Bucking The System

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Python 101 (part 7): Dinner With A Hungry Giant - Mercury Rising


    (Page 2 of 9 )

    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 else: 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") 37.0 >>> temperature.tempConv(37, "c") 98.6 >>> temperature.tempConv(37, "d") 'Cannot convert!' >>>

    More Python Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    PYTHON ARTICLES

    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT