Python
  Home arrow Python arrow Page 7 - Python 101 (part 7): Dinner With A Hun...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PYTHON

Python 101 (part 7): Dinner With A Hungry Giant
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    - 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
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT