How to convert PyObject to java boolean type

你离开我真会死。 提交于 2019-12-11 04:26:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!