Multiple inheritance
hard 50 minA class with several parent classes gets the types, the methods and the fields of all of them.
Watch lesson4:37
Out of inheritance
When a side can end.
The point: a parent is for life. People graduate, change jobs and have two jobs. That is a list of roles.
A person with roles
1/4One class of people
No Student and no Employee under Person. A human stays one object for life.
has (lives on)implements
class Person: def __init__(self, name): self.name = name self._roles = {} def take_role(self, kind, role): self._roles[kind] = role def leave_role(self, kind): return self._roles.pop(kind, None) def role(self, kind): return self._roles.get(kind)class StudentRole: def __init__(self, school): self.school = schoolclass EmployeeRole: def __init__(self, company): self.company = companynoor = Person("Noor")noor.take_role(StudentRole, StudentRole("Harbour College"))noor.take_role(EmployeeRole, EmployeeRole("Blue Cafe"))noor.leave_role(StudentRole) # graduated, same personprint(noor.role(StudentRole), noor.role(EmployeeRole).company) # None Blue CafeClasses against roles
| Sides | Join classes, at most | Role classes |
|---|---|---|
| 2 | 1 | 2 |
| 3 | 4 | 3 |
| 4 | 11 | 4 |
| 5 | 26 | 5 |
Moving a live system
- 1
Add roles beside classes
Person gets
role(kind). WorkingStudent answers with itself. - 2
Move callers
From
isinstance(p, Student)top.role(StudentRole). - 3
Move data
One row per role in the store, with start and end date.
- 4
Make persons
The factory returns Person with roles. The join classes are used by nobody.
- 5
Delete
Join classes first, then Student and Employee.
A side with a life of its own