logo       

Re: To stupid to live??? Problems with coercing non-numeric types.: msg#00053

lang.jython.user

Subject: Re: To stupid to live??? Problems with coercing non-numeric types.


The approach Jack suggested seems to distort the fact that we're dealing with an object, not a string. Even though it's an object that wraps a string, it's still not a string.

However, if you implemented an __str__ method, it should be possible to convert it to a string or java.lang.String object. How about:

x.myMethod(y, str(z))

or

from java.lang import String
s = String(str(z))
x.myMethod(y, s)

My guess is that it's too much of a leap to go from a Jython object that wraps a string to a Java object the wraps a string. With a little help, like that shown above, you should be able to bridge the gap.

Regards,
Todd Moyer



Alex Pollitt wrote:
Hi all,

Apologies if I'm being too stupid to live, but I'm having problems calling Java methods from within Jython.

Specifically I have a method that takes a string one of its parameters. I can call this fined from python if I pass in a string literal:

x.myMethod(y, "a string")

But, if I pass in a python object:

z = Z("a string")
x.myMethod(y, z)

Then I have defined then I get "TypeError: myMethod(): 2nd arg can't be coerced to String".

Z is a very simple wrapper class that wraps a string. (In my real code it does more then this, but for this example all it does is wrap a string.)

class StringValue:
def __init__(self, text):
self.text = text
return

def __repr__(self):
return repr((str(self.__class__), self.text))

def __str__(self):
return self.text

def __coerce__(self, other):
try:
result = coerce(str(self), other)
except TypeError:
result = None
return result

So all of the following work fine with Z:

print z #calls z.__str__()
print repr(z) #calls z.__repr__()
print "z is " + z + "." #calls z.__coerce__()

But the x.myMethod(y, z) does not work, and does not call z.__coerce__() either.

Any help much appreciated!

Thanks in advance,
Alex.







-------------------------------------------------------
This SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first software developer in space?
Enter now for the Oracle Space Sweepstakes!
http://ads.osdn.com/?ad_id=7412&alloc_id=16344&op=click


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise