Message5598

Author vgod
Recipients vgod
Date 2010-03-28.03:13:15
SpamBayes Score 5.886702e-11
Marked as misclassified No
Message-id <1269745997.21.0.121112940843.issue1584@psf.upfronthosting.co.za>
In-reply-to
Content
I am trying to dynamically create functions in __main__ from a Jython module. The idea is illustrated as following.


---- module.py ----
class Foo:
  def func(self):
    pass

def setup():
  foo = Foo()
  __main__.__dict__["func"] = foo.func # redirects a global function "func" to an instance method foo.func

setup()
---- end of module.py ---

---- main.py -----
from module import *

func() # this should invokes aObject.func

---- end of main.py ----

I ran the above code simply using the following java code.

PythonInterpreter py = new PythonInterpreter();
py.execfile("main.py");
py.cleanup();

When I was running this code first time, everything worked fine. The weird thing was, if I ran the same thing again, I got a "NameError: name 'func' is not defined". 
I did some experiments and found I couldn't write anything into __main__ from a module after the first run. I tried to find a workaround, e.g. reload(module), but still failed.
History
Date User Action Args
2010-03-28 03:13:17vgodsetrecipients: + vgod
2010-03-28 03:13:17vgodsetmessageid: <1269745997.21.0.121112940843.issue1584@psf.upfronthosting.co.za>
2010-03-28 03:13:16vgodlinkissue1584 messages
2010-03-28 03:13:15vgodcreate