5.5. Inheritance Multiple

5.5.1. Diagram

../../_images/inheritance-usecase-multiple.png

5.5.2. Code

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


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


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


class User(Account):
    pass


class Staff(Account, TupleSerializer):
    pass


class Admin(Account, TupleSerializer, DictSerializer):
    pass

5.5.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'}

5.5.4. Use Case - 0x01

>>> class Account:
...     def login(self): ...
...     def logout(self): ...
>>>
>>> class ManagesUsers:
...     def user_add(self): ...
...     def user_edit(self): ...
...     def user_delete(self): ...
>>>
>>> class ManagesStaff:
...     def staff_add(self): ...
...     def staff_edit(self): ...
...     def staff_delete(self): ...
>>>
>>>
>>> class User(Account):
...     pass
>>>
>>> class Staff(Account, ManagesUsers):
...     pass
>>>
>>> class Admin(Account, ManagesUsers, ManagesStaff):
...     pass

5.5.5. Use Case - 0x02

>>> class UserPermissions:
...     pass
>>>
>>> class AdminPermissions:
...     pass
>>>
>>> class MyAccount(UserPermissions, AdminPermissions):
...     pass