class Person: def __init__(self, name, age, birthdate, children): self.name = name self.set_age(age) self.__set_birthdate(birthdate) self.__children = children def print_children(self): print("children are:") for c in self.__children: print('child =', c.name) 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.") joels_children = [] p = Person("Joel", 65, (1954, 11, 4), joels_children) #p1 = Person("Joel", 65, (1954, 11, 4)) p.age = 66 p.age_incr = 10 print('age =', p.age) print('name =', p.name) print('name =', p.get_name()) print('birthdate =', p.birthdate) q = Person("Alice", 6, (2009, 11, 4), []) p.print_children() joels_children.append(q) joels_children.append(5) p.print_children() """ if p == ("Joel", 65, (1954, 11, 4)): print("People are equal.") else: print("Not equal.") """