newbie: concatenate literals (using jython)

D

davidmichaelkarr

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?
 
F

Fredrik Lundh

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>
 
T

Tomasz Lisowski

Fredrik said:
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
 
R

razornl

Note that
"getListenPort()" returns an int. Is that a problem?

yes, that's a problem. Python needs strings for concatenation
apparently. you can use the .toString() methon on the int to convert it
to a string ;)

a=1
s='hello'
print s+a

TypeError: __add__ nor __radd__ defined for these operands


print s+a.toString()
hello1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top