5.4. Inheritance Multilevel

5.4.1. Diagram

../../_images/inheritance-usecase-multilevel.png

5.4.2. Code

class Account:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname


class User(Account):
    pass


class Staff(User):
    def astuple(self):
        data = vars(self).values()
        return tuple(data)


class Admin(Staff):
    def asdict(self):
        data = vars(self)
        return dict(data)

5.4.3. Usage

>>> mark = User('Mark', 'Watney')
>>>
>>> mark.astuple()
Traceback (most recent call last):
AttributeError: 'User' object has no attribute 'astuple'
>>>
>>> mark.asdict()
Traceback (most recent call last):
AttributeError: 'User' object has no attribute 'asdict'
>>> rick = Staff('Rick', 'Martinez')
>>>
>>> rick.astuple()
('Rick', 'Martinez')
>>>
>>> rick.asdict()
Traceback (most recent call last):
AttributeError: 'Staff' object has no attribute 'asdict'
>>> melissa = Admin('Melissa', 'Lewis')
>>>
>>> melissa.astuple()
('Melissa', 'Lewis')
>>>
>>> melissa.asdict()
{'firstname': 'Melissa', 'lastname': 'Lewis'}