[Tutor] module objects
Fri, 28 Dec 2001 13:43:08 +0100
On 0, Karthik Gurumurthy <email@example.com> wrote:
> i remember reading that a module in python is also an object.
> so if i define a file test.py , i have a test module object.
> i observed that sys.modules is a dict containing many modules.
> if i add my module also to this dictionary what can i achieve?
> i guess am not able to put it properly but my question is
> that what can i do with it? some examples w'd help.
> import test
> import sys
> sys.modules['test'] = test
The first time you do 'import test', the module is loaded and automatically
put into sys.modules (so it's of no use to do it by hand).
If you later import test again (say, in another module), it's simply
retrieved from sys.modules and doesn't have to be loaded from the file all
This means that if you had put it into sys.modules as some other name, you
could have imported that name as well:
sys.modules['test2'] = test
print test is test2 # prints 1