tuples and cartesian coordinates

Bruno Desthuilliers bdesth.tagada at tsoin-tsoin.free.fr
Wed Dec 17 16:47:58 EST 2003


Gerrit Holl wrote:
> Hi,
> 
> the FAQ says:
> 
> 
>>For example, a Cartesian coordinate is appropriately represented as a
>>tuple of two or three numbers.
> 
> 
> I find it strange to use tuples for a coordinate. After all, a
> coordinate represents a position of an object. Suppose I have a game
> where the player has a position: isn't it stupid to use tuples rather
> than lists or another type (maybe complex numbers?), because I want to
> be able to change the position?
> 
> Shouldn't coordinates be mutable?

Coordinates are used to identify a point in a plane or space. The fact 
that some object move from point A to point B doesn't mean that point A 
has moved to point B !-)

class Movable:
   def __init__(self, initialPosition):
     self.position = initialPosition

   def moveTo(self, newPosition):
     self.position = newPosition

   def moveBy(self, deltaX, deltaY):
     self.position = (self.position[0] + deltaX,
                      self.position[1] + deltaY)

   def getPosition(self):
     return self.position

   def getPositionX(self):
     return self.position[0]

   def getPositionY(self):
     return self.position[1]

points = [(x, y) for x in range(10) for y in range(10)]

m = Movable(points[0])
m.moveTo(points[55])
m.moveBy(-1, 1)
m.getPosition()
-> (4,6)

Bruno





More information about the Python-list mailing list