logo       

Re: Special method names problem?: msg#00007

lang.jython.user

Subject: Re: Special method names problem?

__int__ is not used in Jython (2.1) for coersion
to ints. Instead, Jython calls the __tojava__ method
to convert the object to a java.lang.Integer. This
works:

>>> import java,org
>>> class C:
... v=0
... def __init__(self,value):
... self.v=value
... def __int__(self):
... return int(self.v)
... def __tojava__(self,t):
... if t is java.lang.Integer:
... return self.__int__()
... return org.python.core.Py.NoConversion
...
>>> c=C(33)
>>> for i in range(c):
... print i
...
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32


Eugene Rosenzweig wrote:
Hi,

What I have in jython is:

>>> class C:
... v=0
... def __init__(self,val):
... self.v=val
... def __int__(self):
... return int(self.v)
... def __chr__(self):
... return chr(self.v)
>>> c=C(33)
>>> print int(c)
33
>>> print chr(c)
Traceback (innermost last):
File "<console>", line 1, in ?
TypeError: chr(): 1st arg can't be coerced to int
>>> for i in range(c):
... print i
...
Traceback (innermost last):
File "<console>", line 1, in ?
TypeError: range(): 1st arg can't be coerced to int

In CPython these statements don't present any problem.

I am quite new to Python and jython.

Eugene.

PS I have looked at the language reference and __chr__ doesnt appear to be in it however removing it from the class definition doesn't alter the results in any way.



-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Jython-users mailing list
Jython-users@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/jython-users


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642


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

News | FAQ | advertise