Python
  Home arrow Python arrow Page 7 - Python 101 (part 7): Dinner With A Hungry Giant
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

Python 101 (part 7): Dinner With A Hungry Giant
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2001-08-07


    Table of Contents:
  • Python 101 (part 7): Dinner With A Hungry Giant
  • Mercury Rising
  • Between A Rock And...Another Rock
  • Love Bytes
  • Enter The Hungry Giant
  • From Python, With Love
  • Doing The Math
  • String Theory (And Other Interesting Stuff)
  • Bucking The System

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Python 101 (part 7): Dinner With A Hungry Giant - Doing The Math
    ( Page 7 of 9 )

    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
     

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek