[Tutor] Re: implementing relationship between objects

Lee Harr missive@hotmail.com
Sat Mar 29 21:09:01 2003


>How would you implement in python a 'relational' 1:1, 1:N, N:N
>relationship between objects?
>
>Of course both 'sides' should know about the other and changing one
>'side' of the relation should affect the other side (kind of >referetial 
>integrity).
>


One thing you might use is a mutable builtin object, like
a list. Two objects might share the same list for instance:


class Rel:
    members = []
    def __init__(self):
        if len(self.members) > 1:
            raise TypeError, "Two members maximum"
        else:
            if self.members:
                self.other = self.members[0]
                self.other.other = self
            self.members.append(self)



I bet you could extend this to the other cases also. I do not
know if this is the best way, or even really a good way. I guess
it depends on what your goal is.


_________________________________________________________________
MSN 8 with e-mail virus protection service: 2 months FREE* 
http://join.msn.com/?page=features/virus