2 ms·
> So a better question might be which version is more flexible or reusable? In fact, inheritance has nothing to do with reusability (or flexibility): it's poin
by BerislavLopac 3y ago
> So a better question might be which version is more flexible or reusable?
In fact, inheritance has nothing to do with reusability (or flexibility): it's point is to provide typing hierarchy, so the code can follow the Liskov substitution principle.
In "traditional" Python, which is dynamically typed and never statically analysed, there were no built-in controls over what is being passed at runtime, so the only real purpose for inheritance was to reuse code. This changeds with the introduction of typing annotations -- while the runtime is still fully dynamic, you can use mypy and other static analysis tools to minimise type-related errors before deploying.
In fact, Python has a great way to both support composition and support interfaces without requiring inheritance: a combination of typing.Protocol and mixins.