On 1 Jan 2007 17:09:19 -0800, [EMAIL PROTECTED] wrote: > Lets say I have those two classes, Person and Address. How would one > implement the relationship between them? First, a Person can have one > or more addresses (or none), that could be represented as a list of > Addresses, right? But then, if I have an Address I want to see which > persons who live there, then I would have a list of Persons in each > Address.
> Is this doubly-linked way of doing it a good way of doing it, or is > there a better "OO way" I haven't learned yet? I don't know about "a better OO" way, but one way is to model the various relationships as a list of (person, address) pairs: persons = [ Person( ), Person( ), Person( ) ] addresses = [ Address( ), Address( ), Address( ), Address( ) ] livesat = ((person[ 1 ], address[ 1 ]), (person[ 2 ], address[ 1 ]), (person[ 2 ], address[ 2 ])) worksat = ((person[ 1 ], address[ 3 ]), (person[ 2 ], address[ 4 ])) Then the list of people who live at address[ 1 ] is: [ (person, address) for (p, a) in livesat if a is address[ 1 ] ] But what about all the various relationships (lives at, works at, is known to have vandalized, etc.)? livesat = Relationship( "lives at" ) worksat = Relationship( "works at" ) vandalized = Relationship( "vandalized" ) information = ((person[ 1 ], address[ 1 ], livesat), (person[ 2 ], address[ 1 ], livesat), (person[ 2 ], address[ 2 ], livesat), (person[ 1 ], address[ 3 ], worksat), (person[ 2 ], address[ 4 ], worksat), (person[ 1 ], address[ 4 ], vandalized)) Now the list of people who live at address[ 1 ] is: [ (person, address) for (p, a, r) in information if a is address[ 1 ] and r is livesat ] (There are other ways, too, such as capturing one person and one address right inside a Relationship; someone with a more comp-sci-theoretical background than I have can probably tell you why or when one way might be better than the other.) The definitions of Person, Address, and Relationship, as well as the user-interface(s) and the persistent storage mechanism(s), are left as exercises to the interested reader. ;-) Regards, Dan -- Dan Sommers <http://www.tombstonezero.net/dan/> "I wish people would die in alphabetical order." -- My wife, the genealogist -- http://mail.python.org/mailman/listinfo/python-list