What is the cleanest way to for a module to access objects from the script that imports it?
Michael L Torrie
torriem at chem.byu.edu
Tue Oct 31 13:02:17 EST 2006
On Fri, 2006-10-27 at 14:53 -0700, noamsml at gmail.com wrote:
> Hi,
>
> I am new to python and am currently writing my first application. One
> of the problems I quickly ran into, however, is that python's imports
> are very different from php/C++ includes in the sense that they
> completely wrap the imported script in a module object. One of the
> problems with this was that a plugin system that I am making requires
> use of objects, classes and the such from the original script. Thus, on
> one hand, I am hesitant to use execfile(), since I *do* want to wrap
> the plugin up, but on the other hand, I want the plugin to be able to
> use functions from the original script. Any ideas?
I have a system that uses modules as plugins also. These are loaded
dynamically when the user specifies them from a gui. I put all these
modules in an array using the __import__ function. I found, though,
that I needed to specify whether or not each module had actually loaded,
or if there had been an exception (module not found or whatever). So I
wrote a wrapper object that would try to load the module and store it as
a local attribute. I made my wrapper object implement functions like
__getattr__ and pass any unknown calls into the module object itself,
making the wrapper object act as if it was the module, but having extra
capabilities, such as being able to tell me if the module had actually
loaded or not.
Michael
>
> Sincerely,
> Noam Samuel.
>
More information about the Python-list
mailing list