Creating "virtual" module-namespace
Carl Banks
imbosol at vt.edu
Wed Dec 11 17:51:20 EST 2002
Simon wrote:
> Imagine... there is a:
>
> a= "class nasenbaer:\n def singt():\n print "tralala"\n\n"
>
> (the content of a is:
> class nasenbaer:
> def singt():
> print "tralala"
>
>
> )
>
> now I want to exec a in a "special" way so that later I can call
>
> exec a in ???????
>
> grail=somthing.nasenbaer()
> grail.singt()
>
> My try was
>
> b=globals()
>
> exec a in b
> grail=b.nasenbaer()
>
> but it failed...
It's because b is a dictionary. b['nasenbaer']() would have worked,
I think, but I don't think it's what you wanted.
> what is the correct way?
I would do it this way. I'm assuming "something" is an object (an
instance of some class) and you want to be able to call
something.nasenbaer().
class whatever: pass
something = whatever()
exec a in globals(), something.__dict__
grail = something.nasenbaer()
--
CARL BANKS
More information about the Python-list
mailing list