tuples and cartesian coordinates

G

Gerrit Holl

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

Bruno Desthuilliers

Gerrit said:
Hi,

the FAQ says:




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
 
P

Peter Otten

Gerrit said:
the FAQ says:


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:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: readonly attribute

Peter
 
S

Steve Horsley

Gerrit said:
Hi,

the FAQ says:




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
setThrusters(fullSpeedAhead)

Steve
 
J

John Roth

Gerrit Holl said:
Hi,

the FAQ says:


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
 
T

Terry Reedy

message
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top