I have a Python file which contains a class. I need to create an instance of that class and be able to call methods in that through Java.
I came up with a solution like this:
PythonInterpreter r = new PythonInterpreter();
r.execfile("File.py");
PyObject p = r.eval("Car().begin()");
System.out.println(p.toString());
And the python code:
class Car(SomeExtendedClass):
myvar = 5
def set(self, my):
self.myvar = my;
def begin(self):
return self.myvar
Now, when I execute this, it prints 5 But if I run the following code:
PyObject p = r.eval("Car().begin()");
System.out.println(p.toString());
r.eval("Car().set(7)");
p = r.eval("Car().begin()");
System.out.println(p.toString());
It will still print 5, instead of 7
It looks like I did not create one instance of Car and it always creating a new instance instead of using the created one.
Am I right?
Is it possible to create a new instance from a class in a Python file, and invoke/get data from methods with Java?
I have tried loading PyInstance using eval() but I get cannot cast exception from it:
return (PyInstance) this.interpreter.eval(className);