calculation on lists

Chris Kaynor ckaynor at zindagigames.com
Wed Dec 19 13:02:18 EST 2012


Chris



On Wed, Dec 19, 2012 at 7:24 AM, loïc Lauréote <laureote-loic at hotmail.fr>wrote:

>  Thank for your answer,
>
> I found something allowing to avoid loops.
> I use operator overloading.
>
>
> import math
>
> class Vector:
>     def __init__(self, x=0, y=0):
>         self.x=x
>         self.y=y
>     def __eq__(self, vB): return (self.x==vB.x) and (self.y==vB.y)
>     def __add__(self, vB):  return Vector(self.x+vB.x,self.y+vB.y)
>     def __sub__(self, vB):  return Vector(self.x-vB.x,self.y-vB.y)
>     def __mul__(self, c):
>         if isinstance(c,Vector): return  Vector(self.x*c.x,self.y*c.y)
>         else: return Vector(c*self.x,c*self.y)
>
>
>     def __div__(self, c):
>         if isinstance(c,Vector): return  Vector(self.x/c.x,self.y/c.y)
>         else: return Vector(c*self.x,c*self.y)
>
>
>
> a = Vector(4,5)
> b = Vector(6,7)
> print a,b
> print b*b+a
>
>
> thx
>
>
Depending on exactly what you are doing, you may also want to look at using
NumPy. It is a highly optimized math library for Python.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121219/fbca6cae/attachment.html>


More information about the Python-list mailing list