Python
  Home arrow Python arrow Page 6 - 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 - From Python, With Love
    ( Page 6 of 9 )

    Thus far, you've been importing modules as is, and using module attributes by referencing them with the module name as prefix. Python also offers an alternative method to selectively access and use module attributes - the "from" statement.

    The "from" statement allows you to import specific attributes from a module into the current namespace. Since these attributes become part of the current namespace, it no longer becomes necessary to prefix them with the module name in order to use them.

    Consider the following example, which demonstrates how this works.


    >>> from menu import lunch This module is owned by The Hungry Giant. Cook smart. Eat healthy. Die anyway. >>> lunch["Tue"] 'Fish and Chips' >>>


    In this case, the module variable "menu.lunch" is imported into the current namespace as the variable "lunch".

    It's important to exercise caution when using the "from" statement - since "from" imports module attributes directly into the current namespace, you run the risk of overwriting current names when you use it. To illustrate this, consider the following simple Python program:


    #!/usr/bin/python

    def getDinnerItem(day): print "Sorry, diner closed on " + day

    getDinnerItem("Mon")


    When you run this, the output reads


    Sorry, diner closed on Mon


    Now, look what happens when you import some names from the "menu.py" module into this program:


    #!/usr/bin/python

    def getDinnerItem(day): print "Sorry, diner closed on " + day

    # imports from menu import dinner from menu import getDinnerItem

    getDinnerItem("Mon")


    When you run this program. the imported names will overwrite the names already existing in the namespace, resulting in the following output:


    This module is owned by The Hungry Giant. Cook smart. Eat healthy. Die anyway. Dinner on Mon is: Pasta


    Another important gotcha with "from": importing names using "from" implies that changes to those names (after they have been imported) are not reflected in the parent module. Consider the following module:


    # numbers.py x = 1 y = 2 z = x + y


    Look what happens when I import these values into a script:


    #!/usr/bin/python

    # import from numbers import x,y,z

    # at this stage, z = x + y => z = 3 print z

    # alter the value of imported x x = 10

    # z is still referring to the value of x in the module, so z still = 3 print z

    # the only way to get z to recognize the new value of x is to redefine z in context z = x + y

    # now z = 12 print z


     
     
    >>> 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek