从java调用带有返回类型的python方法

时间:2012-02-21 05:05:46

标签: java python call

我试图从Java类调用Python方法。方法名称为toEvaluate(Expression),位于evaluator.py。此方法返回一个布尔值,并接受一个字符串作为输入参数。此外,此方法toEvaluate(Expression)不是类方法。 (我在网上找到了这个代码。)

但是,此代码与我的代码有两个主要区别:

  1. run是一种类方法而toEvaluate(Expression)不是

  2. run没有参数,toEvaluate(Expression)将表达式作为输入。

  3. 有人可以建议一些类似的代码来解决我的问题吗?

    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();  
    
            ie.execfile("hello.py");  
    
            PyInstance hello = ie.createClass("Hello", "None");  
    
            hello.invoke("run");  
        }  
    }  
    

    Python代码:

    class Hello:  
        __gui = None  
    
        def __init__(self, gui):  
             self.__gui = gui  
    
        def run(self):  
            print 'Hello world!'
    

    这是toEvaluate的代码,它调用解析器并接收后缀表达式结果。

    def toEvaluate(expression):
        a = []
        global input
        input = expression+"$"
        infixExp()
        for i in infixexp:
            a.append(i)
            exp = parser.infixtopostfix(a)  
        return parser.evalExp(exp)
    

0 个答案:

没有答案
相关问题