tuples and cartesian coordinates

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

  1. Gerrit Holl

    Gerrit Holl Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. Gerrit Holl

    Peter Otten Guest

    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 ?
    TypeError: readonly attribute

    Peter
    Peter Otten, Dec 17, 2003
    #3
  4. 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
    setThrusters(fullSpeedAhead)

    Steve
    Steve Horsley, Dec 17, 2003
    #4
  5. Gerrit Holl

    John Roth Guest

    "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
    #5
  6. Gerrit Holl

    Terry Reedy Guest

    "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
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Suresh  Kumar
    Replies:
    0
    Views:
    577
    Suresh Kumar
    Jul 3, 2003
  2. Suresh  Kumar
    Replies:
    0
    Views:
    506
    Suresh Kumar
    Jul 4, 2003
  3. ldries46
    Replies:
    8
    Views:
    987
    Nobody
    Apr 18, 2013
  4. Bart Braem
    Replies:
    5
    Views:
    116
    Kenosis
    Aug 23, 2006
  5. Jon Reyes
    Replies:
    18
    Views:
    216
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page