Python
  Home arrow Python arrow Page 5 - Python 101 (part 7): Dinner With A Hungry Giant
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

Python 101 (part 7): Dinner With A Hungry Giant
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 - Enter The Hungry Giant
    ( Page 5 of 9 )

    The first time a module is imported, the code within it is automatically executed. This comes in handy if you need to initialize variables, or print a copyright notice:


    # menu.py

    # set up dictionaries breakfast = {'Mon':'Ham and Eggs', 'Tue':'Grilled Sandwiches', 'Wed':'Spanish Omelettes', 'Thu':'Bacon and Eggs', 'Fri':'Pancakes', 'Sat':'Scrambled Eggs', 'Sun':'Coffee and Donuts'}

    lunch = {'Mon':'Russian Salad', 'Tue':'Fish and Chips', 'Wed':'Chicken Curry', 'Thu':'Egg Salad', 'Fri':'Cheeseburgers', 'Sat':'Steak', 'Sun':'Stir-fried Chicken'}

    dinner = {'Mon':'Pasta', 'Tue':'Thai Noodles', 'Wed':'Pork Chops', 'Thu':'Prawns in Butter Garlic Sauce', 'Fri':'Fried Fish', 'Sat':'Mongolian Chicken', 'Sun':'Vegetable Stew'}

    # functions to return menu items based on day def getBreakfastItem(day): print "Breakfast on " + day + " is: " + breakfast[day]

    def getLunchItem(day): print "Lunch on " + day + " is: " + lunch[day]

    def getDinnerItem(day): print "Dinner on " + day + " is: " + dinner[day]

    def generateMenu(day): print "Breakfast on " + day + " is: " + breakfast[day] print "Lunch on " + day + " is: " + lunch[day] print "Dinner on " + day + " is: " + dinner[day]

    print "This module is owned by The Hungry Giant. Cook smart. Eat healthy. Die anyway."



    >>> import menu This module is owned by The Hungry Giant. Cook smart. Eat healthy. Die anyway. >>> import menu >>> import menu >>> import menu >>>


    Note that the code within the module is only executed the first time; subsequent attempts to import the module do not execute the code within it.

    In case you need to re-run the module code, Python offers the reload() function, which reloads a module and executes the code within it again. When a module is reloaded, all module attributes are refreshed with their original values.

    In order to illustrate this, let's import the "menu.py" module above and access one of its attributes.


    >>> import menu This module is owned by The Hungry Giant. Cook smart. Eat healthy. Die anyway. >>> menu.breakfast["Fri"] 'Pancakes' >>>


    Next, let's alter this attribute.


    >>> menu.breakfast["Fri"] = "Jam and Toast" >>> menu.breakfast["Fri"] 'Jam and Toast' >>>


    Note how re-importing the module has no effect whatsoever on the changed attribute,


    >>> import menu >>> menu.breakfast["Fri"] 'Jam and Toast' >>>


    while reloading it resets all attributes back to their initial values.


    >>> reload(menu) This module is owned by The Hungry Giant. Cook smart. Eat healthy. Die anyway. >>> menu.breakfast["Fri"] 'Pancakes' >>>


    The reload() function only works if the module has been successfully imported prior to calling it. An attempt to reload() a module which has not been previously imported will result in an error.


    >>> # module not yet imported >>> reload(menu) Traceback (innermost last): File "", line 1, in ? NameError: menu >>> # import it... >>> import menu This module is owned by The Hungry Giant. Cook smart. Eat healthy. Die anyway. >>> # and now try reloading it! >>> reload(menu) This module is owned by The Hungry Giant. Cook smart. Eat healthy. Die anyway. >>>


    The reload() function comes in handy if a module changes after it has been imported; it provides a quick and easy way to update the namespace during program execution.

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

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek