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.
Next: From Python, With Love >>
More Python Articles
More By Vikram Vaswani, (c) Melonfire