# tuples and cartesian coordinates

Discussion in 'Python' started by Gerrit Holl, Dec 17, 2003.

1. ### Gerrit HollGuest

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?

yours,
Gerrit.

--
231. If it kill a slave of the owner, then he shall pay slave for slave
to the owner of the house.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

Gerrit Holl, Dec 17, 2003

2. ### Bruno DesthuilliersGuest

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

Bruno Desthuilliers, Dec 17, 2003

3. ### Peter OttenGuest

Gerrit Holl wrote:

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

One benefit of immutable coordinates: you can safely assign one coordinate
to multiple positions:

class Shape: pass
s1 = Shape()
s2 = Shape()

origin = ImmutableCoord(0, 0)
s1.position = origin
s2.position = origin

Whereas with mutable coordinates you would need

class Shape:
def __init__(self, pos):
self.pos = MutableCoord(pos)
def setPosition(self, pos)
self.pos.x = pos.x
self.pos.y = pos.y
s1.setPosition(origin)
s2.setPosition(origin)

There are several variants of this scheme, but I think they all tend to
complicate the design, because not only rebinding of pos needs to be
tracked, but also the changing of pos.x and pos.y.

By the way, complex numbers are immutable:

>>> z = 1 + 3j
>>> z.imag = 5

Traceback (most recent call last):
File "<stdin>", line 1, in ?

Peter

Peter Otten, Dec 17, 2003
4. ### Steve HorsleyGuest

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?
>
> yours,
> Gerrit.
>

Depends. Does it make logical sense for the coordinate to be mutable
in the context of your app?

If an object is returning an instrumentation value, as in
positionNow = spaceship.getPosition()
then it does make sense to be mutable. Of course the spaceship itself
may well keep a mutable position internally that it changes in
response to

Steve

Steve Horsley, Dec 17, 2003
5. ### John RothGuest

"Gerrit Holl" <> wrote in message
news:...
> 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?

I would think not. I look at them as Value Objects: that is,
objects which serve to encapsulate a value and which don't
have any identity outside of that function.

So if you're not going to create an object for them, I'd
regard a tuple as perfectly adequate.

John Roth
>
> yours,
> Gerrit.
>
> --
> 231. If it kill a slave of the owner, then he shall pay slave for slave
> to the owner of the house.
> -- 1780 BC, Hammurabi, Code of Law
> --
> Asperger's Syndrome - a personal approach:
> http://people.nl.linux.org/~gerrit/english/
>

John Roth, Dec 18, 2003
6. ### Terry ReedyGuest

"Gerrit Holl" <> wrote in
message
news:...
> Shouldn't coordinates be mutable?

Coordinates no, positions possibly yes, as long as
you avoid the mutiple reference trap others have
described. My philosophy on the tuple vs. list
debate is that people should use what works for
their particular application.

TJR

Terry Reedy, Dec 18, 2003