Re: How to call Python function that takes a parameter

"Stefan Richthofer" <[email protected]> Wed, 30 Dec 2015 16:26:31 +0100
Newsgroups gmane.comp.lang.jython.user
Message-ID <trinity-0cb8ae22-9705-4a56-8fbc-408e503cc031-1451489191001@3capp-gmx-bs49>
Subodh,

let me state that I did not test your code for an exact solution.

However just by taking a look, it occurs to me that the main

method does never load your Python code. A call like

ie.execfile([fileWithYourPythonCode].py)

would be needed before the class can be created etc.

Also I don't think that "None" as a String is an appropriate argument

where a PyObject is expected. Use Py.None instead.

That said, consider to coerce your Python class into a Java interface,

which would allow a smoother integration with Java code.

Hope, this somewhat helps...

-Stefan

Gesendet: Dienstag, 29. Dezember 2015 um 23:11 Uhr

Von: "Subodh Garg-SSI" <[email protected]>

An: "[email protected]" <[email protected]>

Betreff: [Jython-users] How to call Python function that takes a parameter

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

------------------------------------------------------------------------------

_______________________________________________
Jython-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-users