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

Diez B. Roggisch deets at nospam.web.de
Sat Oct 28 03:22:54 CEST 2006


noamsml at gmail.com schrieb:
> 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?

If you really have a C++ background, you should be aware that this 
language requires each and every bit of declarations to be known 
beforehand. Which means you need stuff to be factorized into header 
files, and include them wherever you want to use things.

Python is waaaay more relaxed in this regard. As long as you only use 
objects, it actually doesn't give a damn  about "knowing" them. if you 
need to instantiate them, there isn't anything wrong about importing a 
main module from a plugin module - you can do that. However, things can 
get messed up if you really do a circular import, meaning that you don't 
have a plugin system with lazy loading, but "real" cycles.

Diez



More information about the Python-list mailing list