Jython operator override question

"[email protected]" <[email protected]>
Newsgroups gmane.comp.lang.jython.user
Message-ID <[email protected]>
Dear all,

Let's look at the sample code for operator override below:

# point.py 
# Operator override test 
class Point(): 
    def __init__(self, x=0, y=0): 
        self.x = x 
        self.y = y 

    def __str__(self): 
        return "({0},{1})".format(self.x, self.y) 

    def __add__(self, other): 
        if isinstance(other, Point): 
            x = self.x + other.x 
            y = self.y + other.y 
        else: 
            x = self.x + other 
            y = self.y + other 
        return Point(x, y) 

#Run test in module 
if __name__ == '__main__': 
    p1 = Point(2,3) 
    p2 = Point(-1,1) 
    p3 = p1 + p2 
    p4 = p1 + 2 
    p5 = 2 + p1 
    print 'p3 = ' + str(p3) 
    print 'p4 = ' + str(p4) 
    print 'p5 = ' + str(p5)


For this "+" operator override sample:
p4 = p1 + 2       is ok
but:
p5 = 2 + p1      will cause error: TypeError: unsupported operand type(s) for +: 'int' and 'instance'

So my question is how to make 'p5 = 2 + p1' run?

Thanks for any help!

Yaqiang


**************************************************
Dr. Yaqiang Wang
Chinese Academy of Meteorological Sciences (CAMS)
46, Zhong-Guan-Cun South Avenue
Beijing, 100081
China

http://www.meteothinker.com
http://hi.baidu.com/meteoinfo
            
[email protected]
***************************************************

------------------------------------------------------------------------------
Dive into the World of Parallel Programming! The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net

_______________________________________________
Jython-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.