[Tutor] module objects

alan.gauld@bt.com alan.gauld@bt.com
Fri, 28 Dec 2001 15:50:05 -0000


> i remember reading that a module in python is also an object.

Almost everything in Python is an object - especially in V2.2!

[ But note that an object is not necessarily the same thing 
  as an instance of a class ]

> so if i define a file test.py , i have a test module object.

Correct.

> i observed that sys.modules is a dict containing many modules.

It contains the list of modules currently imported.

> if i add my module also to this dictionary what can i achieve?

Not a lot! Python uses that list, its not really for programmers 
to mess with unless you really know what you're doing - and even 
then you probably shouldn't!

You should only add to the list by importing your module as in:

import test

> that what can i do with it? some examples w'd help.

You can do spome freaky things to python's internal 
operations, but you probably shouldn't...

> import test
> import sys
> sys.modules['test'] = test

Is redundant since the import already did that for you.

Alan g.