How to convert PyObject to java boolean type

I would like to throw org.python.core.PyObject in java.lang.Boolean. Something like:

boolean i = ((Boolean) PyObject).booleanValue(); 
+2
java python casting jython boolean
source share
2 answers

Just try the following:

 PyObject obj = interpreter.eval("True"); boolean i = ((PyInteger) obj).asInt() != 0; 
+2
source share

You should use a non- Python standard interface interface:

 PyObject obj = interpreter.eval("True"); boolean i = obj.__nonzero__(); 

(it's called "non-zero" because it existed before Python was of a Boolean type, and Guido's ways are sometimes mysterious)

0
source share

All Articles