I am using the code from the answered question found here.
What would be the most pythonic way to store information about marriage? I would like it to be possible to ask for self.husband or self.wife from any married person stored in a class, and ask for self.children from these recursively.
class Person:
ID = itertools.count()
def __init__(self, name, parent=None, level=0):
self.id = self.__class__.ID.next() # next(self.__class__.ID) in python 2.6+
self.parent = parent
self.name = name
self.level = level
self.children = []
def createTree(d, parent=None, level=0):
if d:
member = Person(d['parent'], parent, level)
level = level + 1
member.children = [createTree(child, member, level) for child in d['children']]
return member
t = createTree(my_tree) # my_tree is the name of thedictionary storing parents and children. Need 'parent' key to become 'parents' key which stores a list of two people.