Home arrow Python arrow 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

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
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:


# menu.py

# set up dictionaries # snip

_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 # 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 >>>


 
 
>>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: