Message5598
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. |
|
Date |
User |
Action |
Args |
2010-03-28 03:13:17 | vgod | set | recipients:
+ vgod |
2010-03-28 03:13:17 | vgod | set | messageid: <1269745997.21.0.121112940843.issue1584@psf.upfronthosting.co.za> |
2010-03-28 03:13:16 | vgod | link | issue1584 messages |
2010-03-28 03:13:15 | vgod | create | |
|