Hi Jose, have you thought about shallow copying? I am learning Python too, so I can be wrong here. But I have just tried the idea and it worked for me:
class celestialBody(object): def __init__(self, m, x, y, z): self.mass = m self.pos = [x, y, z] def __str__(self): return str(self.mass) + ' and ' + str(self.pos) earth = celestialBody(1, 0, 0, 0) jupiter = celestialBody(2, 2, 2, 2) asteroid = celestialBody(0.00001, 1, 1, 1) planetSystem = [earth, jupiter] positionVectors = [earth.pos, jupiter.pos] print "object earth's mass and original position is", earth # Now we make changes to earth's coordinates one by one # not by manipulating earth but by manipulating the positionVectors positionVectors[0][0] = positionVectors[0][0] + 0.01*asteroid.pos[0] positionVectors[0][1] = positionVectors[0][1] + 0.01*asteroid.pos[1] positionVectors[0][2] = positionVectors[0][2] + 0.01*asteroid.pos[2] print "object earth's new position, as seen in positionVectors, is", positionVectors[0] print "object earth's mass and new position, as seen in earth object, is", earth print "object earth's mass and new position, as seen in planetSystem, is", planetSystem[0] >>> ====================== RESTART ====================== >>> object earth's mass and original position is 1 and [0, 0, 0] object earth's new position, as seen in positionVectors, is [0.01, 0.01, 0.01] object earth's mass and new position, as seen in earth object, is 1 and [0.01, 0.01, 0.01] object earth's mass and new position, as seen in planetSystem, is 1 and [0.01, 0.01, 0.01] >>> As to Alan's ideas below, I don't really understand them. Trung Doan ============ On Sun, Jan 20, 2013 at 5:01 AM, Alan Gauld <alan.ga...@btinternet.com>wrote: > On 19/01/13 15:47, Jose Amoreira wrote: > > motion. I defined a class, CelestialBody, that describes objects that >> represent planets in my simulation. These objects have three attributes: >> position, velocity and mass (the first two are 3D-vectors; as such, the >> number of attributes is actually 7). The many-body system is represented >> in the simulation by a list of CelestialBody objects. >> > > OK, why not hold that list in the CelestialBody class? > > Then get the list to either hold a copy of the key values or generate it > on demand. If the list of values is also in the class definition all the > descendant types of body can update the class list at the same time as > updating their own copy. It means duplicating data but it keeps the > instances in control of the data while making it available (read-only, from > the class) when needed. The overhead is creating the getter/setter methods > to update the class list in parallel with the instance data. > > Alternatively store it once at class level and create properties of the > instance that do all access in the class list. That way it looks like you > are updating the instance but the instance delegates the storage to the > class list. The instance can store its own index into the class list as > advised by the class on creation. > > There are ways of doing what you want that keep responsibility in the > object. The choice will depend on how often you need to vary the instance > values as opposed to using the class list. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ______________________________**_________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor> >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor