In a graphical program I\'m writing using pygame I use a tuple representing a coordinate like this: (50, 50).
Sometimes, I call a function which returns another tupl
This is a work in progress as I am learning Python myself. Can we use classes here, could simplify some operations later. I propose to use a coord class to store the coordinates. It would override add and sub so you could do addition and subtraction by simply using operators + and -. You could get the tuple representation with a function built into it.
Class
class coord(object):
def __init__(self,x,y):
self.x = x
self.y = y
def __add__(self,c):
return coord(self.x + c.x, self.y + c.y)
def __sub__(self,c):
return coord(self.x - c.x, self.y - c.y)
def __eq__(self,c): #compares two coords
return self.x == c.x and self.y == c.y
def t(self): #return a tuple representation.
return (self.x,self.y)
Usage
c1 = coord(4,3) #init coords
c2 = coord(3,4)
c3 = c1 + c2 #summing two coordinates. calls the overload __add__
print c3.t() #prints (7, 7)
c3 = c3 - c1
print c3.t() #prints (3, 4)
print c3 == c2 #prints True
you could improve coord to extend other operators as well (less than, greater than ..).
In this version after doing your calculations you can call the pygame methods expecting tuples by just saying coord.t(). There might be a better way than have a function to return the tuple form though.
To get your "+" and "+=" behaviour you can define your own class and implement the __add__()
method. The following is an incomplete sample:
# T.py
class T(object):
def __init__(self, *args):
self._t = args
def __add__(self, other):
return T(*([sum(x) for x in zip(self._t, other._t)]))
def __str__(self):
return str(self._t)
def __repr__(self):
return repr(self._t)
>>> from T import T
>>> a = T(50, 50)
>>> b = T(3, -5)
>>> a
(50, 50)
>>> b
(3, -5)
>>> a+b
(53, 45)
>>> a+=b
>>> a
(53, 45)
>>> a = T(50, 50, 50)
>>> b = T(10, -10, 10)
>>> a+b
(60, 40, 60)
>>> a+b+b
(70, 30, 70)
EDIT: I've found a better way...
Define class T as a subclass of tuple and override the __new__
and __add__
methods. This provides the same interface as class tuple
(but with different behaviour for __add__
), so instances of class T can be passed to anything that expects a tuple.
class T(tuple):
def __new__(cls, *args):
return tuple.__new__(cls, args)
def __add__(self, other):
return T(*([sum(x) for x in zip(self, other)]))
def __sub__(self, other):
return self.__add__(-i for i in other)
>>> a = T(50, 50)
>>> b = T(3, -5)
>>> a
(50, 50)
>>> b
(3, -5)
>>> a+b
(53, 45)
>>> a+=b
>>> a
(53, 45)
>>> a = T(50, 50, 50)
>>> b = T(10, -10, 10)
>>> a+b
(60, 40, 60)
>>> a+b+b
(70, 30, 70)
>>>
>>> c = a + b
>>> c[0]
60
>>> c[-1]
60
>>> for x in c:
... print x
...
60
40
60
List comprehension is probably more readable, but here's another way:
>>> a = (1,2)
>>> b = (3,4)
>>> tuple(map(sum,zip(a,b)))
(4,6)
Well, one way would be
coord = tuple(sum(x) for x in zip(coord, change))
If you are doing a lot of math, you may want to investigate using NumPy, which has much more powerful array support and better performance.
As John Y mentions, this is pretty easy using numpy.
import numpy as np
x1 = (0,3)
x2 = (4,2)
tuple(np.add(x1,x2))
My two cents, hope this helps
>>> coord = (50, 50)
>>> change = (3, -5)
>>> tuple(sum(item) for item in zip(coord, change))
(53, 45)