Home arrow Python arrow Page 3 - Python 101 (part 7): Dinner With A Hungry Giant

Between A Rock And...Another Rock - 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
The elements within a module - variables, functions, other imports - are referred to as "module attributes".

The "import" keyword is used to search for the named module and load it if found. Once the module has been successfully imported, functions and variables contained within it can be accessed by prefixing their names with the module name. Since each module sets up an independent namespace, this is a handy way to avoid name clashes.

To see how this works, consider the following example, which uses two modules, each containing a function named rock():


def rock(): # module huey.py print "Between a rock and a hard place"



def rock(): # module dewey.py print "Rock on!"


Let's see what happens when I try to use these in a program:


>>> import huey >>> import dewey >>> huey.rock() Between a rock and a hard place >>> dewey.rock() Rock on! >>>


Since each module attribute has a unique prefix, it's possible to distinguish between attributes with the same name in different modules, and thereby avoid name conflicts.

You'll be interested to know that everything you do in Python takes place within the context of a module. Even commands typed into the interactive command-line interpreter take place within the bubble of Python's top-level module, named "__main__".

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