问题
I would like to cast org.python.core.PyObject to java.lang.Boolean. Something similar to:
boolean i = ((Boolean) PyObject).booleanValue();
回答1:
Just try the following:
PyObject obj = interpreter.eval("True");
boolean i = ((PyInteger) obj).asInt() != 0;
回答2:
You should use the Python standard object interface nonzero method:
PyObject obj = interpreter.eval("True");
boolean i = obj.__nonzero__();
(it's called "nonzero" because it existed before Python had a boolean type and Guido's ways are mysterious sometimes)
来源:https://stackoverflow.com/questions/9387694/how-to-convert-pyobject-to-java-boolean-type