What is the cleanest way to for a module to access objects from the script that imports it?

Fuzzyman fuzzyman at gmail.com
Tue Oct 31 18:42:28 EST 2006


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

This is a situation I've been in before when writing plugins - you need
some way of 'connecting' the plugin to the main application.

I usually provide a baseclass which the user should subclass to
implement the plugin.

The override something like an 'onActivate' method, which when called,
is passed in (by the application) all the information they need.

They can also optionally override 'onLoad' and 'onUnload' or whatever
other methods you want to provide.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

> Sincerely,
> Noam Samuel.




More information about the Python-list mailing list