This recursive function (search_Bases) would hopefully iterate through each base class and __init__ it. How do I refer to each class’s self, without actually using self? I’ve tried a couple things but I can’t figure it out. When I change the Child() class up to do something similar, it works. So I have no clue what to do next.
def search_Bases(child=0):
if child.__bases__:
for parent in child.__bases__:
parent.__init__(self) # <-----I can't figure out how to initiate the class
# without referring to 'self'....
search_Bases(parent)
class Female_Grandparent:
def __init__(self):
self.grandma_name = 'Grandma'
class Male_Grandparent:
def __init__(self):
self.grandpa_name = 'Grandpa'
class Female_Parent(Female_Grandparent, Male_Grandparent):
def __init__(self):
Female_Grandparent.__init__(self)
Male_Grandparent.__init__(self)
self.female_parent_name = 'Mother'
class Male_Parent(Female_Grandparent, Male_Grandparent):
def __init__(self):
Female_Grandparent.__init__(self)
Male_Grandparent.__init__(self)
self.male_parent_name = 'Father'
class Child(Female_Parent, Male_Parent):
def __init__(self):
Female_Parent.__init__(self)
Male_Parent.__init__(self)
#search_Bases(Child)
child = Child()
print child.grandma_name
I don’t think you properly understand class inheritance. In Python,
means that
Female_ParentIS-AMale_Grandparent, which seems unlikely. What you meant to say wasThis also has problems, in that the role changes depending on who is asking – by definition, a
Male_Grandparent(of his grandchildren) is also aMale_Parent(of his children) who is also aChild(of his parents).You can boil all your classes down to
and derive further relationships from there. This gives a much simpler structure, without the point-of-view contradictions, but still results in problems evaluating further relationships because a given person’s links only go “up” – a given person knows who their parents were, but can’t identify their children.
You could keep a list of all your Persons and search the list each time (like a mother going around the kindergarten saying, “Are you my child? You? Are YOU my child?”) but this seems very inefficient.
Instead, you can make each relationship two-way – each parent has a list of all their children and each child has a list of all their parents. It makes adding and removing people a little harder, but is well worth it.
The following is longer than I like but as short as I could make it; it should suit your needs much better!
and now, in action: