class Person: def __init__(self, name, age, birthdate, children): self.name = name self.set_age(age) self.__set_birthdate(birthdate) self.__children = list() for c in children: self.new_child(c) def print_children(self): print("children are:") for c in self.__children: print(' child =', c.name) def new_child(self, child): if not isinstance(child, Person): raise TypeError('child must be a Person') self.__children.append(child) def set_age(self, age): if not isinstance(age, int): raise TypeError('age must be a positive int') elif age < 0: raise ValueError('age must be a positive int') self.__age = age def get_age(self): return self.__age def incr_age(self, val): self.__age += 1 age = property(get_age, set_age) age_incr = property(get_age, incr_age) def __set_birthdate(self, birthdate): #validate thate birthdate is a valid birthdate if not isinstance(birthdate, tuple): raise TypeError('birthdate must be a tuple with 3 values') elif len(birthdate) != 3: raise ValueError('birthdate must be a tuple with 3 values') #format is (year, month, day) self.__BIRTHDATE = birthdate def get_birthdate(self): return self.__BIRTHDATE birthdate = property(get_birthdate) def get_home_planet(self): return "Earth" home_planet = property(get_home_planet) def set_name(self, name): self.name = name def get_name(self): return self.name def __eq__(x, y): '''two people are equal if their names and birthdates are the same''' if isinstance(y, Person): if x.name.lower() == y.name.lower() and x.__BIRTHDATE == y.__BIRTHDATE: return True else: return False elif isinstance(y, tuple) and len(y) == 3: if x.name.lower() == y[0].lower() and x.__BIRTHDATE == y[2]: return True else: return False else: raise TypeError("Person can only be compared to another person.") alice = Person("Alice", 6, (2009, 11, 4), []) try: joel = Person("Joel", 65, (1954, 11, 4), [alice]) joel.new_child(joel) except: print('err') print('name =', joel.name) joel.print_children() joel.new_child(alice) joels_children.append(alice) joels_children.append(1) joels_children.append(13.5) joels_children.append((2009, 11, 4)) joel.print_children()