Why COMPOSITION is better than INHERITANCE - detailed Python example
Video Statistics and Information
Channel: ArjanCodes
Views: 53,137
Rating: 4.9554086 out of 5
Keywords: composition vs inheritance, composition vs inheritence, composition vs inheritance python, favor composition over inheritance, favor composition, python software design, software design principles, software design, design patterns, inheritance or composition, software design principles solid, software design principles loose coupling, software design principles and concepts, software design principles cohesion, software design principles in software engineering, inheritance
Id: 0mcP8ZpUR38
Channel Id: undefined
Length: 23min 29sec (1409 seconds)
Published: Fri Jun 11 2021
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.
I have mixed feelings about this. It's advice you'll hear repeated often, and is often good advice for many isolated projects, but I think the video only scratches the surface of design aspects when it comes to composition vs inheritance.
The topic gets particularly more complex when you think about how code sharing works in a real ecosystem where you may be consuming code from upstream that you have no control of and/or you may be designing code intended to be used by other projects downstream.
I don't think it's useful to generically flatten the argument to say "always favor composition". You have to look at the use case and figure out which might work better based on the project details and the goals you have in mind, particularly when it comes to code reuse.
I personally find that, when writing code that is going to be consumed by others, designing for inheritance (which does not necessarily require using it extensively) ends up making many projects most flexible for downstream consumers. How you write you compose your class and its methods can greatly impact what downstream users are able to easily change for their own unique use-cases that you'll never be able to cover in your own codebase.
As Raymond Hettinger once put it shortly: