merging the global namespaces of two modules

Diez B. Roggisch deets at nospam.web.de
Thu May 22 09:16:55 EDT 2008


Fabrizio Pollastri wrote:

> Jeff wrote:
>> Can you be more specific?  modA and modB don't import from each other
>> but both need to access objects in the global namespace of what
>> module?  The controlling application?
> 
>> Or do you mean that, for
>> example, modA needs to access some functions in modB, but does not
>> have import statements to do so, and you want the controlling program
>> to do the imports for it?
> 
> Exactly this.
> 
>> If that is the case, that is bad
>> programming practice in Python.  Python is not C and you can't just
>> share header files :)
> 
> Ok. I know that I want a python import to behave like a C include, it is
> so bad?
> 
> The motivation arise from a family of applications that import, among
> others, two modules: a basic one and a specific one. The specific one is
> chosen from a set of modules at import time depending on other imported
> modules, so the basic module can known its external references against the
> specific module only at run time.
> 
> One solution can be:
> -the application imports the basic module,
> -the basic module selects the proper specific module among the allowable
> specific modules,
> -the basic module imports the selected module with an __import__ call
> equivalent to "import selected_module_X as select_module",
> -Any reference to the specific module in the basic module is written in
> the form "selected_module.object_name".
> 
> Any better solution?

http://peak.telecommunity.com/DevCenter/PkgResources#namespace-package-support

Diez



More information about the Python-list mailing list