Wednesday, June 16, 2010

Modules used in Python

A module looks  much like your normal python program.
We import bits of it (or all of it) into other programs.

To import all the variables, functions and classes from file1.py into another program you are writing, we use the import operator. For example, to import file2.py into your main program, you would have this:
Code Example
### mainprogam.py
### IMPORTS ANOTHER MODULE
import moduletname

continue ..........
......
......

More module thingummyjigs (in lack of a better title)

Wish you could get rid of the modulename. part that you have to put before every item you use from a  module? No? Never? Well, I'll teach it you anyway. One way to avoid this hassle is to import only the wanted objects from the module. To do this, you use the from operator. You use it in the form of from modulename import itemname. Here is an example:
Code Example importing individual objects
### IMPORT ITEMS DIRECTLY INTO YOUR PROGRAM

# import them
from modulename import itemname
from modulename import itemname

No comments:

Post a Comment