HomePython 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
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'}
# 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.