Creating "virtual" module-namespace
Steve Holden
sholden at holdenweb.com
Wed Dec 11 18:06:31 EST 2002
I'm not sure this will help, but I observe that the namespace to be provided
is a dictionary. Thus a minimalist approach would be:
>>> b = {}
>>> exec a in b
>>> type(b)
<type 'dict'>
What you need next is therefore
>>> grail = b['nasenbaer']()
Then you will see you have a nasenbaer instance in grail. However ...
>>> grail.singt()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: singt() takes no arguments (1 given)
this error is because you forgot to put the "self" argument on the method's
argument list :-) I presume, though, that you are really interested in
getting nasenbaer in a namespace rather than a simple dictionary. You now
have some further food for thought...
regards
-----------------------------------------------------------------------
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/pwp/
Previous .sig file retired to www.homeforoldsigs.com
-----------------------------------------------------------------------
"Simon" <simon.12.ghum at spamgourmet.com> wrote in message
news:Xns92E1EDB538B97simon12ghumspamgourm at 62.153.159.134...
> 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...
>
> what is the correct way?
More information about the Python-list
mailing list