24
loading...
This website collects cookies to deliver better user experience
class Ticket:
def __init__(self, traveler_name: str, origin: str, destination: str, class_: EconomicClasses, date_: str) -> None:
self.traveler_name = traveler_name
self.origin = origin
self.destination = destination
self.date_ = date_
self.class_ = class_
__init__
method. The first argument that we pass into __init__
(by convention called self) is telling the class that any values assigned to this, is added to the specific paper we're holding in our hands, not to the class as a whole. More technically speaking, we're passing in the instance as an argument to the initialiser function def __init__
. So, when I then create an instance of the class like so: my_ticket = Ticket(traveler_name='Mr. Swarm', origin='Los Angeles', destination='New York', class_=EconomicClasses.COACH, date_='2021-12-24')
the airline agent is writing the information I'm giving him over the phone on the form he just ripped off of his notepad.class Ticket:
def __init__(self, traveler_name: str, origin: str, destination: str, class_: EconomicClasses, date_: str) -> None:
...
self.class_ = class_
self.distance = get_distance(self.origin, self.destination)
self.availability = get_availability(self.origin, self.destination, self.date_)
self.distance
as well as self.availability
) although using instance variables as arguments for those functions, this because those values aren't necessarily only needed when calculating the price of one ticket. Other parts of your software might want to use distance to calculate fuel costs or availability to determine what campaigns should be used. Visualise this as your air line agent grudgingly finding out the distance and availability in two tables separate from your ticket form, using his ash tray as a ruler, because after all, he is a maverick. Once he has found those values, he enters them into the little formula they have to calculate the price, which he then does using one of those calculators that prints out the numbers on a small roll of paper, but with the paper long gone.class Ticket
def __init__(self, traveler_name: str, origin: str, destination: str, class_: EconomicClasses, date_: str) -> None:
...
self.price = self.calculate_price()
def calculate_price(self):
return round(self.distance * 0.3 * self.class_.value * self.filled_rate, 2)
class Ticket:
...
def mail_ticket(self, street: str, number: str, zip_code: str) -> None:
mailing_info = '\n'.join([self.traveler_name, street, number, zip_code])
print(mailing_info)
class
is the notepad with all the forms used by the airline agent when selling a ticket. Really just blanks waiting to be filled out, either directly or by calculations. The individual forms are the class instances
. The minimum information asked for in order to even create a ticket, are the instance variables
that are assigned during the initialisation (__init__()
) method. Note that you don't have to initialise any particular variables in order to create a class instance. The methods are then the functions within the class. Methods can be called when the instance is initialised or as a separate step, depending on whether the execution of the method is optional or not.