How to call Python function that takes a parameter
Subodh Garg-SSI <[email protected]> Tue, 29 Dec 2015 22:11:45 +0000
| Newsgroups | gmane.comp.lang.jython.user |
|---|---|
| Message-ID | <A5C6690323A97B4894F900E78517A9EE1B08A93B@SSIEXCH-MB2.ssi.samsung.com> |
Hi All,
Here is the example Python code in hello.py and Java code. I am able to invoke run function of Python class Hello as given the code. I need help in calling "run2" function of Python in Java.
Thanks,
Subodh
class Hello:
__gui = None
def __init__(self, gui):
self.__gui = gui
def run(self):
print 'Hello world!'
def run2(myname):
print myname
Here is my Java code:
import org.python.core.PyInstance;
import org.python.core.PyString;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import java.lang.*;
import java.util.*;
public class InterpreterExample
{
PythonInterpreter interpreter = null;
public InterpreterExample()
{
PythonInterpreter.initialize(System.getProperties(),
System.getProperties(), new String[0]);
this.interpreter = new PythonInterpreter();
}
void execfile( final String fileName )
{
this.interpreter.execfile(fileName);
}
PyInstance createClass( final String className, final String opts )
{
return (PyInstance) this.interpreter.eval(className + "(" + opts + ")");
}
public static void main( String gargs[] )
{
InterpreterExample ie = new InterpreterExample();
PyInstance hello = ie.createClass("Hello", "None");
hello.invoke("run");
}
}
------------------------------------------------------------------------------
_______________________________________________
Jython-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-users