Home arrow Python arrow Page 5 - Python 101 (part 7): Dinner With A Hungry Giant

Enter The Hungry Giant - Python

Python allows developers to logically group functions togetherinto modules, which can be imported and used by any Python program. In thisarticle, find out what a module is, learn how modules and module namespaceswork, and check out the default modules that ship with Python

TABLE OF CONTENTS:
  1. Python 101 (part 7): Dinner With A Hungry Giant
  2. Mercury Rising
  3. Between A Rock And...Another Rock
  4. Love Bytes
  5. Enter The Hungry Giant
  6. From Python, With Love
  7. Doing The Math
  8. String Theory (And Other Interesting Stuff)
  9. Bucking The System
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 5
August 07, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: