[英]Preserving the Java-type of an object when passing it from Java to Jython
I wonder if it possible to not have jython automagicaly transform java objects to python types when you put them in a Java ArrayList. 我想知道当你把它们放在Java ArrayList中时,是否有可能没有 jython automagicaly将java对象转换为python类型。
Example copied from a jython-console: 从jython-console复制的示例:
>>> b = java.lang.Boolean("True");
>>> type(b)
<type 'javainstance'>
>>> isinstance(b, java.lang.Boolean);
1
So far, everything is fine but if I put the object in an ArrayList 到目前为止,一切都很好,但如果我把对象放在ArrayList中
>>> l = java.util.ArrayList();
>>> l.add(b)
1
>>> type(l.get(0))
<type 'int'>
the object is transformed into a python-like boolean (ie an int) and... 该对象被转换为类似python的布尔值(即int)和...
>>> isinstance(l.get(0), java.lang.Boolean)
0
which means that I can no longer see that this was once a java.lang.Boolean. 这意味着我再也看不到这曾经是java.lang.Boolean。
Clarification 澄清
I guess what really want to achieve is to get rid of the implicit conversion from Java-types to Python-types when passing objects from Java to Python. 我想真正想要实现的是在将对象从Java传递到Python时摆脱从Java类型到Python类型的隐式转换。 I will give another example for clarification. 我将举一个澄清的例子。
A Python module: 一个Python模块:
import java
import IPythonModule
class PythonModule(IPythonModule):
def method(self, data):
print type(data);
And a Java-Class that uses this module: 以及使用此模块的Java类:
import java.util.ArrayList;
import org.python.core.PyList;
import org.testng.annotations.*;
import static org.testng.AssertJUnit.*;
public class Test1 {
IPythonModule m;
@BeforeClass
public void setUp() {
JythonFactory jf = JythonFactory.getInstance();
m = (IPythonModule) jf.getJythonObject(
"IPythonModule",
"/Users/sg/workspace/JythonTests/src/PythonModule.py");
}
@Test
public void testFirst() {
m.method(new Boolean("true"));
}
}
Here I will see the output 'bool' because of the implicit conversion, but what I would really like is to see 'javainstance' or 'java.lang.Boolean'. 在这里,我将看到输出'bool',因为隐式转换,但我真正想要的是看到'javainstance'或'java.lang.Boolean'。 If you want to run this code you will also need the JythonFactory-class that can be found here . 如果要运行此代码,还需要可在此处找到的JythonFactory类。
You appear to be using an old version of Jython. 您似乎使用旧版本的Jython。 In current Jython versions, the Python bool
type corresponds to a Java Boolean
. 在当前的Jython版本中,Python bool
类型对应于Java Boolean
。
Jython is not transforming the Java type to a Python type on the way into the ArrayList
- on the contrary, it will transform a primitive Python type to a primitive or wrapper Java type when passing it to a Java method, and a Java type to a Python type on the way out. Jython在进入ArrayList
过程中没有将Java类型转换为Python类型 - 相反,它将原始Python类型转换为原始或包装Java类型,同时将其传递给Java方法,Java类型转换为Python类型在出路上。
You can observe this by printing the contents of the array. 您可以通过打印数组的内容来观察这一点。 Note that the Python bool
is capitalized ( True
); 请注意,Python bool
是大写的( True
); the Java Boolean
is not. Java Boolean
不是。
>>> from java.lang import Boolean
>>> b = Boolean('True')
>>> b
true
>>> from java.util import ArrayList
>>> l = ArrayList()
>>> l.add(b)
True
>>> l
[true]
>>> l.add(True)
True
>>> l
[true, true]
>>> list(l)
[True, True]
If this still doesn't do what you want, consider writing a small Java helper function that examines the array for you without conversion. 如果这仍然没有达到你想要的效果,可以考虑编写一个小的Java辅助函数,在没有转换的情况下为你检查数组。 It's arguably a bug that Jython doesn't automatically convert the Boolean
you constructed into a Python bool
, and in this case it gives you no advantage over using Boolean.TRUE
or the Python True
. 这可能是一个错误,Jython不会自动将你构造的Boolean
转换为Python bool
,在这种情况下,它没有使用Boolean.TRUE
或Python True
优势。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.