HomePython Page 7 - Python 101 (part 7): Dinner With A Hungry Giant
Doing The Math - 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
With these caveats in mind, the "from" statement provides a convenient way to import specific bits of a module into another program. Most of the time, it's used in connection with modules containing a large number of different functions; it's easier - and more optimal - to simply import the functions you need, rather than the entire module. Here's an example, using the built-in "math" module:
>>> from math import sqrt, exp
>>> sqrt(256)
16.0
>>> exp(0)
1.0
>>>
If you have good reason to do so, or simply like to experiment, you can use "from" to import everything from a module into the current namespace - here's how:
>>> from menu import *
This module is owned by The Hungry Giant. Cook smart. Eat healthy. Die
anyway.
>>> dinner
{'Fri': 'Fried Fish', 'Tue': 'Thai Noodles', 'Thu': 'Prawns in Butter
Garlic Sauce', 'Sun': 'Vegetable Stew', 'Wed': 'Pork Chops', 'Mon':
'Pasta', 'Sat': 'Mongolian Chicken'}
>>> lunch
{'Fri': 'Cheeseburgers', 'Tue': 'Fish and Chips', 'Thu': 'Egg Salad',
'Sun': 'Stir-fried Chicken', 'Wed': 'Chicken Curry', 'Mon': 'Russian
Salad', 'Sat': 'Steak'}
>>> breakfast
{'Fri': 'Pancakes', 'Tue': 'Grilled Sandwiches', 'Thu': 'Bacon and Eggs',
'Sun': 'Coffee and Donuts', 'Wed': 'Spanish Omelettes', 'Mon': 'Ham and
Eggs', 'Sat':'Scrambled Eggs'}
>>> getLunchItem("Wed")
>>> 'Chicken Curry'
>>>
If you need to prevent certain module attributes from being imported with a "from module import *" statement, you can prefix the attribute name within the module with an underscore. This is a primitive technique, but it does work - as the following example demonstrates:
# functions to return menu items based on day
# snip
Now, I will be unable to access the "_dinner" attribute when I use "from" to import everything,
>>> from menu import *
This module is owned by The Hungry Giant. Cook smart. Eat healthy. Die
anyway.
>>> lunch["Tue"]
'Fish and Chips'
>>> _dinner["Tue"]
Traceback (innermost last):
File "", line 1, in ?
NameError: _dinner
>>> dinner["Tue"]
Traceback (innermost last):
File "", line 1, in ?
NameError: dinner
>>>
although I will still be able to access it when I perform an "import" operation.
>>> import menu
This module is owned by The Hungry Giant. Cook smart. Eat healthy. Die
anyway.
>>> menu._dinner
{'Fri': 'Fried Fish', 'Tue': 'Thai Noodles', 'Thu': 'Prawns in Butter
Garlic Sauce', 'Sun': 'Vegetable Stew', 'Wed': 'Pork Chops', 'Mon':
'Pasta', 'Sat': 'Mongolian Chicken'}
>>>
It's also possible for modules to import each other - here's a "circle" module which uses functions imported from the "math" module.
# circle.py
def area(r):
from math import pi
area = pi * r * r
return area
>>> import circle
>>> circle.area(5)
78.5398163397
>>>