Hoisting common functionality into a base class

Now we have the base Aircraft class we can refactor by hoisting into it other common functionality. For example, both the initializer and registration() methods are identical between the two subtypes:

class Aircraft:

def __init__(self, registration):
self._registration = registration

def registration(self):
return self._registration

def num_seats(self):
rows, row_seats = self.seating_plan()
return len(rows) * len(row_seats)


class AirbusA319(Aircraft):

def model(self):
return "Airbus A319"

def seating_plan(self):
return range(1, 23), "ABCDEF"


class Boeing777(Aircraft):

def model(self):
return "Boeing 777"

def seating_plan(self):
# For simplicities sake, we ignore complex
# seating arrangement for first-class
return range(1, 56), "ABCDEGHJK"

These derived classes only contain the specifics for that aircraft type. All general functionality is shared from the base class by inheritance.

Thanks to duck-typing, inheritance is less used on Python than in other languages. This is generally seen as a good thing because inheritance is a very tight coupling between classes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset