Creating "virtual" module-namespace

Erik Max Francis max at alcyone.com
Thu Dec 12 03:22:39 EST 2002


Harald wrote:

> I have some modules stored in strings (to be exact: in a database).
> 
> Now I want to use THIS modules in the same way as if I would import
> them
> via
> import FileName
> 
> If I simply exec this strings in "globals"
> 
> exec <string> in globals()
> 
> all the classes & functions are in my global namespace - without a
> prefix.

Attribute access is related to a dictionary by an instance with a
__dict__ attribute.  You could try something like

	class Dummy: pass
	namespace = Dummy() # we just need an instance of _something_
	exec code in namespace.__dict__
	namespace.nasenbaer()

How's that grab you?

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ To perceive is to suffer.
\__/ Aristotle
    Official Omega page / http://www.alcyone.com/max/projects/omega/
 The official distribution page for the popular Roguelike, Omega.



More information about the Python-list mailing list