|
From: Kevin B. <kb...@ca...> - 2001-06-30 04:48:15
|
???
public class A
{
public Object get()
{
return "Hello";
}
public void doit( String s )
{
System.out.println( s );
}
}
$ jython
Jython 2.1a1 on java1.3.1 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> import A
>>> a = A()
>>> o = a.get()
>>> o
'Hello'
>>> type( o )
<jclass org.python.core.PyString at 4729773>
>>> a.doit( o )
Hello
>>> type( "" )
<jclass org.python.core.PyString at 4729773>
>>>
jython converts Java String objects into python strings, and transparently
converts them back into Java strings if you call a method that requires
them.
You may have problems if you put them in a jarray, a Collection, etc., but
just passing Strings should work fine.
Or you may have problems if you want to do String method ops on the
returned value:
>>> o.length()
Traceback (innermost last):
File "<console>", line 1, in ?
AttributeError: 'string' object has no attribute 'length'
So you can construct a new Java String from the Python string object:
>>> from java.lang import *
>>> o2 = String( o )
>>> o2.length()
5
Does that help?
kb
Roman Milner wrote:
> >>>>> "BZ" == Brian Zhou <bri...@ya...> writes:
>
> BZ> In dynamic languages like Python/Smalltalk/Scheme, you usually
> BZ> don't need cast. Because "the methods a particular instance
> BZ> can perform" becomes the type of the instance, it's not as
> BZ> important which class that instance actually belongs. At
> BZ> instantiation time, "all the methods the instance can perform"
> BZ> is determined. At runtime, you either can call the method or
> BZ> you get an AttributeError.
>
> What if you need to pass an object to a java method that requires it
> to be cast to a different java type?
>
> What if one java method gives me an Object back, but it is actually a
> String. I have another java method that requires a String argument. Is
> there a way to do that cast in jython?
>
> I ran in to this when trying to use the Java xml-rpc library from
> jython.
>
> ^Roman
>
> _______________________________________________
> Jython-dev mailing list
> Jyt...@li...
> http://lists.sourceforge.net/lists/listinfo/jython-dev
|