newbie: concatenate literals (using jython)

Tomasz Lisowski list at notmyisp.pl
Fri Dec 23 02:40:40 EST 2005


Fredrik Lundh wrote:
> davidmichaelkarr at gmail.com wrote:
> 
> 
>>I'm using Jython (actually WebLogic WLST), and trying to do something
>>really simple.  I want to create a string from two function calls and a
>>literal, like:
>>
>>  serverport = server.getListenAddress() + ':' + server.getListenPort()
>>
>>This complains:
>>
>>TypeError: __add__ nor __radd__ defined for these operands
>>
>>I've looked at various references, and this seems like the way I'm
>>supposed to do this, but I'm obviously missing something.  Note that
>>"getListenPort()" returns an int.  Is that a problem?
> 
> 
> yes.  python's string concatenation operator doesn't convert things nilly-
> willy (should "1"+1 be 2 or "11" ?).  to convert an object to a string, use
> str(obj).  see this tutorial section for more info:
> 
> http://docs.python.org/tut/node9.html#SECTION009100000000000000000
> 
> </F>
> 
> 
> 
You are right, Fredrik, but David is using Jython, so perhaps he tried 
to mimic the Java language behaviour, where adding ints and strings is 
perfectly valid :)

I admit, though, that I do not know much about Jython apart from the 
fact, that it is somehow related to Java ...

Best regards,
Tomasz Lisowski



More information about the Python-list mailing list