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

Reply via email to