Python
  Home arrow Python arrow Page 5 - 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
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 / 4
    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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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

    - 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
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway