Re: Jython operator override question

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

Thanks for your helpful infromation!

Regards
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]
***************************************************
 
From: Jonathan Beavers
Date: 2015-01-02 18:07
To: wangyq
CC: jython-users
Subject: Re: [Jython-users] Jython operator override question
Hello,
 
I believe your Point class will need to implement the “__radd__” method:
 
>     def __radd__(self, other):
>         return Point.__add__(self, other)
 
If it helps, here’s the available “__r*__” methods:
 
https://docs.python.org/2/reference/datamodel.html#object.__radd__
 
And the “__i*__” methods allow you to use “+=” and friends:
 
https://docs.python.org/2/reference/datamodel.html#object.__iadd__ 
 
Hope this helps!
 
Jon
 
 
> On Jan 2, 2015, at 12:34 AM, [email protected] wrote:
> 
> 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

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