On Tuesday 17 September 2002 19:53, Peter.Rupp@... wrote:
> I have a few questions regarding type conversions between Jython and
> Java, I hope you can help :-)
> 1) How can I specify a java "Null" type in Jython? The constructors
> and methods to several classes I use take Nulls as optional variable
Use None in python to pass null to Java.
> 2) I was using Java's BigDecimal and BigInteger classes. It appears
> easy to create a BigInteger or BigDecimal from a jython string, however=
> performing math functions against them using native numeric Jython
> integers or floats doesn't work (but maybe that's ok?)
To have the standard operators work you'll need a python object
that defines the corresponding methods for emulating python types.
For addition this means you need to define __add__(self, other).
You can do this both in python and in java. In python
it could look like:
from java.?? import BigDecimal # you know better than me which package.
def __init__(self, initialValue =3D 0):
# call appropriate superclass constructor, eg:
def __add__(self, otherBigDecimal):
# Use appropriate java method to add.
# You may have to use a BigInteger attribute instead
# of inheritance to make this work.
def __str__(self): # allow printing
one =3D PythonBigDecimal(1)
print one + one
I java it's quite similar, but you need to inherit from=20
org.python.core.PyObject or PyNumber if there is one.
For BigInteger it might be easier to use the python long type=20
> 3) Do you publish the general rules/semantics regarding automatic type
> conversions between native Jython/Python types and Java variables?
> I'm just not sure what types of Jython variables I can use in Java
> constructors and methods, and if and when they are converted to
> appropriate Java types?
> 4) When I create an instance of a Java type, running Jython's
> "type(<variable>) against the instance variable shows me
> <jclass.org.python.core.PyJavaInstance at <hex #>>
> Is there a way to determine the real Java class type?
There is, but I'm not sure. In jython you might try: