From: Samuele P. <pe...@in...> - 2002-03-21 11:31:56
|
This works: import org.python.core.*; import org.python.util.*; public class Test { public static void main(String[] argv) { try { PySystemState.initialize(); // PythonInterpreter interp = new PythonInterpreter(); // add new 'classes' module to sys.modules PyModule classes = imp.addModule("classes"); String defCode = "from java.lang import Object\n" + "class C(Object): pass"; // same as: exec defCode in classes.__dict__ Py.exec(new PyString(defCode),classes.__dict__,null); Class cl = (Class)classes.__findattr__("C").__tojava__(Class.class); System.out.println(cl); Object c = cl.newInstance(); System.out.println(c); } catch(Exception e) { e.printStackTrace(); } } } The point is that the Python class inheriting from Java should be defined in a module that is present in sys.modules or that can be imported (because Jython retrieves the Python class corresponding to a Java side proxy class by module/name), that means also 'import' the code with the definition would work, and other approaches using more or less Java-side vs Python-side code and for example the 'new' module. The 'main' module in which a PythonInterpreter executes code is not put in sys.modules, also because there would be a conflict with multiple intepreters. regards. |