# SortPersons Example # If we add a __lt__ method to a class, then an array # of objects from this class becomes sortable. class Person: 'Represents a person with a name, gender, and age' # __init__ method is called whenever a new Person # object is created. def __init__(self, the_name, the_gender, the_age): self.name = the_name self.gender = the_gender self.age = the_age # Regular method. def have_birthday(self): self.age += 1 # Define return value for str method. def __str__(self): return f"{self.name};{self.gender};{self.age}" # The __repr__ method supplies the "official" # representation of objects from the class we # are defining. If this method is not defined, a # Person object is represented like this: # # For __repr__, we are using the same representation # as __str__ def __repr__(self): return str(self) # Add < and == operators to the Person class. # Adding < makes Person objects sortable. def __lt__(self, other): return self.age < other.age def __eq__(self, other): return self.age == other.age