From: Samuele P. <pe...@in...> - 2002-03-12 23:12:37
|
From: Oti <oh...@ya...> > Hello, > > I just stumbled over the following behaviour of string.replace(). > Is it true I cannot pass a java.lang.String as first argument: > > [appl|pwe] > jython > Jython 2.1 on java1.3.0 (JIT: null) > Type "copyright", "credits" or "license" for more information. > >>> import string > >>> from java.lang import String > >>> old = String( "old" ) > >>> string.replace( old, "l", "d" ) > Traceback (innermost last): > File "<console>", line 1, in ? > File "e:\jython21\Lib\string.py", line 409, in replace > TypeError: replace(): expected 2 args; got 3 > >>> string.replace( old.intern(), "l", "d" ) > 'odd' > > Yes, the odd behavior is expected. You should consider that string.py is coded assuming Python string input, and simply using the Python string object methods. so string.replace(s,old,new,maxsplit) simply calls s.replace(old,new,maxsplit) now perchance Java strings have a replace method which takes only 2 arguments. The other thing to consider is that Java constructors called from Jython do not convert the result, its Java instance nature is preserved. OTOH normal Java methods have their results always converted following the usual rules (eg. java.lang.String -> string): >>> from java.lang import String >>> s=String("java") >>> s.__class__ <jclass java.lang.String at 8070355> >>> type(s) <jclass org.python.core.PyJavaInstance at 6040101> >>> s2=s.toString() >>> type(s) == type('') 0 >>> type(s2) == type('') 1 The following also fails: >>> s+'__' Traceback (innermost last): File "<console>", line 1, in ? TypeError: __add__ nor __radd__ defined for these operands I don't know if we should change all this. regards. |