我认为您没有正确理解类继承。在 Python 中,
class Female_Parent(Female_Grandparent, Male_Grandparent):
def __init__(self):
表示Female_Parent
IS-A Male_Grandparent
,这似乎不太可能。你想说的是
class Female_Parent(object):
def __init__(self, female_grandparent, male_grandparent):
这也有问题,因为角色的变化取决于谁在问 - 根据定义,Male_Grandparent
(他的孙子孙女的)也是Male_Parent
(他的孩子的)谁也是Child
(他的父母的)。
您可以将所有课程归结为
class Person(object):
def __init__(self, mother, father):
并从那里获得进一步的关系。这给出了一个更简单的结构,没有观点矛盾,但仍然导致评估进一步关系的问题,因为给定人的链接只会“上升” - 给定人知道他们的父母是谁,但不能识别他们的父母孩子们。
您可以保留所有人员的列表并每次搜索该列表(就像一位母亲在幼儿园周围说:“你是我的孩子吗?你吗?你是我的孩子吗?”)但这似乎非常低效。
相反,您可以将每种关系设为双向 - 每个父母都有一个包含所有孩子的列表,每个孩子都有一个包含所有父母的列表。它使添加和删除人员变得更加困难,但非常值得。
以下内容比我喜欢的要长,但我可以做到尽可能短;它应该更适合您的需求!
class Person(object):
def __init__(self, name, sex, parents=None, children=None):
"""
Create a Person
"""
self.name = name
self.sex = sex # 'M' or 'F'
self.parents = set()
if parents is not None:
for p in parents:
self.add_parent(p)
self.children = set()
if children is not None:
for c in children:
self.add_child(c)
def add_parent(self, p):
self.parents.add(p)
p.children.add(self)
def add_child(self, c):
self.children.add(c)
c.parents.add(self)
def __str__(self):
return self.name
def __repr__(self):
return "Person('{}', '{}')".format(self.name, self.sex)
#
# Immediate relationships
#
# Each fn returns a set of people who fulfill the stated relationship
#
def _parent(self):
return self.parents
def _sibling(self):
return set().union(*(p.children for p in self.parents)) - set([self])
def _child(self):
return self.children
def _female(self):
if self.sex=='F':
return set([self])
else:
return set()
def _male(self):
if self.sex=='M':
return set([self])
else:
return set()
def relation(self, *rels):
"""
Find the set of all people who fulfill the stated relationship
Ex:
self.relation("parent", "siblings") # returns all aunts and uncles of self
"""
# start with the current person
ps = set([self])
for rel in rels:
# each argument is either a function or a string
if callable(rel):
# run the function against all people in the current set
# and collect the results to a new set
ps = set().union(*(rel(p) for p in ps))
else:
# recurse to evaluate the string
do = Person._relations[rel]
ps = set().union(*(p.relation(*do) for p in ps))
return ps
def print_relation(self, *rels):
print ', '.join(str(p) for p in self.relation(*rels))
#
# Extended relationships
#
# Supplies the necessary information for Person.relation() to do its job -
# Each key refers to a recursive function tree (nodes are string values, leaves are functions)
#
# (Unfortunately this table cannot be created until the Person class is finalized)
#
Person._relations = {
"parent": (Person._parent,),
"mother": (Person._parent, Person._female),
"father": (Person._parent, Person._male),
"sibling": (Person._sibling,),
"sister": (Person._sibling, Person._female),
"brother": (Person._sibling, Person._male),
"child": (Person._child,),
"daughter": (Person._child, Person._female),
"son": (Person._child, Person._male),
"grandparent": ("parent", "parent"),
"grandmother": ("parent", "mother"),
"grandfather": ("parent", "father"),
"aunt": ("parent", "sister"),
"uncle": ("parent", "brother"),
"cousin": ("parent", "sibling", "child"),
"niece": ("sibling", "daughter"),
"nephew": ("sibling", "son"),
"grandchild": ("child", "child"),
"grandson": ("child", "son"),
"granddaughter": ("child", "daughter")
}
现在,在行动:
mm = Person('Grandma', 'F')
mf = Person('Grandpa', 'M')
m = Person('Mom', 'F', [mm, mf])
fm = Person('Nana', 'F')
ff = Person('Papi', 'M')
f = Person('Dad', 'M', [fm, ff])
me = Person('Me', 'M', [m, f])
s = Person('Sis', 'F', [m, f])
joe = Person('Brother-in-law', 'M')
s1 = Person('Andy', 'M', [s, joe])
s2 = Person('Betty', 'F', [s, joe])
s3 = Person('Carl', 'M', [s, joe])
me.print_relation("grandmother") # returns 'Nana, Grandma'
me.print_relation("nephew") # returns 'Andy, Carl'