473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

tuples and cartesian coordinates

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/

Jul 18 '05 #1
5 3381
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

Jul 18 '05 #2
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
Jul 18 '05 #3
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
Jul 18 '05 #4

"Gerrit Holl" <ge****@nl.linux.org> wrote in message
news:ma*************************************@pytho n.org...
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/

Jul 18 '05 #5

"Gerrit Holl" <ge****@nl.linux.org> wrote in
message
news:ma*************************************@pytho n.org...
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
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Jeff Wagner | last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what you can do with them. I am still left with a question and that is, when should you choose a list or...
5
by: Ã…smund Kveim Lie | last post by:
Hi, We have found a possible bug in 7.3.1. It seems that using CROSS JOIN and doing plain Cartesian product, listing to tables in the from clause, gives different results. According to the...
7
by: Eric Slan | last post by:
Hello All: I'm having a problem that's been baffling me for a few days and I seek counsel here. I have an Access 2000 DB from which I want to run several reports. These reports are...
66
by: Mike Meyer | last post by:
It seems that the distinction between tuples and lists has slowly been fading away. What we call "tuple unpacking" works fine with lists on either side of the assignment, and iterators on the...
44
by: Christoph Zwerschke | last post by:
In Python, it is possible to multiply a string with a number: >>> "hello"*3 'hellohellohello' However, you can't multiply a string with another string: >>> 'hello'*'world' Traceback (most...
78
by: wkehowski | last post by:
The python code below generates a cartesian product subject to any logical combination of wildcard exclusions. For example, suppose I want to generate a cartesian product S^n, n>=3, of that...
27
by: seberino | last post by:
Please help me think of an example where immutable tuples are essential. It seems that everywhere a tuple is used one could just as easily use a list instead. chris
10
by: victor.herasme | last post by:
Hi Everyone, i have another question. What if i wanted to make n tuples, each with a list of coordinates. For example : coords = list() for h in xrange(1,11,1): for i in xrange(1, 5, 1) :...
0
by: raylopez99 | last post by:
keywords: logical coordinates, page coordinates, world coordinates, device coordinates, physical coordinates, screen coordinates, client coordinates. offset rectangle. WYSIWYG rubber rectangle...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.