Now we'll show how it's quite possible to write nice object-oriented code without needing classes. We have a requirement to produce boarding cards for our passengers in alphabetical order. However, we realize that the Flight
class is probably not a good home for details of printing boarding passes. We could go ahead and create a BoardingCardPrinter
class, although that is probably overkill. Remember that functions are objects too and are perfectly sufficient for many cases. Don't feel compelled to make classes without good reason.
Rather than have a card printer query all the passenger details from the flight, we'll follow the object-oriented design principle of "Tell! Don't Ask." and have the Flight tell a simple card printing function what to do.
First the card printer, which is just a module level function:
def console_card_printer(passenger, seat, flight_number, aircraft): output = "| Name: {0}" \ " Flight: {1}" \...