From: brian z. <bz...@zi...> - 2002-03-26 19:06:16
|
Anthony, Try: print "text" + str(obj) for the first one to work. The reason the latter works is because you are not concatenating the values only printing them in order. Python is a strongly typed language and typecasts do not occur automatically for such operations as adding a string to an integer. I do not expect the Jython implementation to change this behaviour. A CPython session exhibits the same: Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> obj = 1 >>> print "text" + obj Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: cannot concatenate 'str' and 'int' objects >>> If you search the c.l.python newsgroup for 'adding string integer' you'll find a number of references to this issue. hope this helps, brian > -----Original Message----- > From: jyt...@li... > [mailto:jyt...@li...] On Behalf > Of Anthony Eden > Sent: Tuesday, March 26, 2002 12:23 PM > To: jyt...@li... > Subject: [Jython-users] Supporting + on Java objects > > > Currently, in Jython, using any arbitrary Java object aside > from a java.lang.String, the following fails: > > print "text" + obj > > With the message: TypeError: __add__ nor __radd__ defined for > these operands > > But the following works: > > print "test", obj > > > Would the Jython implementers consider modifying the > PyJavaInstance class with something similar to: > > public PyObject __add__(PyObject object) { > return __str__ + object.__str__(); > } > > So that the + notation as used in Java works properly? I am > not sure if the code I provided above is correct as I am not > intimately familiar with the Jython source. I am also not > sure if this has been discussed before: the mailing list > archives provided at the Jython.org site do not have search > facilities (AFAICT) and I was unable to find anything about > it in my mail application (back to 7/21/2001), so I apologize > ahead of time if it has. > > Sincerely, > Anthony Eden > > > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users > |