Йордан обнови решението на 01.04.2013 02:27 (преди над 11 години)
+from collections import defaultdict
+import weakref
+
+
+class KeepRefs(object):
+ __refs__ = defaultdict(list)
+
+ def __init__(self):
+ self.__refs__[self.__class__].append(weakref.ref(self))
+
+ @classmethod
+ def get_instances(cls):
+ for inst_ref in cls.__refs__[cls]:
+ inst = inst_ref()
+ if inst is not None:
+ yield inst
+
+
+class Person(KeepRefs):
+ def __init__(self, name="", birth_year=0, gender="",
+ mother=None, father=None):
+ super(Person, self).__init__()
+ self.name = name
+ self.birth_year = birth_year
+ self.gender = gender
+ self.mother = mother
+ self.father = father
+
+ def get_siblings(self, sex):
+ result = []
+ for human in Person.get_instances():
+ if (human is not self and
+ human.gender == sex and
+ ((self.mother is human.mother and self.mother is not None) or
+ (self.father is human.father and self.father is not None))):
+ result.append(human)
+ return result
+
+ def get_brothers(self):
+ return self.get_siblings("M")
+
+ def get_sisters(self):
+ return self.get_siblings("F")
+
+ def children(self, gender=None):
+ result = []
+ for human in Person.get_instances():
+ if (human is not self and
+ (human.father is self or human.mother is self) and
+ (human.gender == gender or gender is None)):
+ result.append(human)
+ return result
+
+ def is_direct_successor(self, stranger):
+ if (self.father is stranger or
+ self.mother is stranger or
+ stranger.father is self or
+ stranger.mother is self):
+ return True
+ else:
+ return False