Delegation versus python inheritance

I just stumbled upon delegation in python and can't plunge into the difference between delegation and inheritance. Why use delegation rather than inheritance?

+3
source share
2 answers

Delegation is a powerful mechanism through which you delegate a task from one class to another. This has the main advantage that the changes in one of your classes will not be cascaded down or into any others.

Also, if you don't get this principle, your classes are probably doing more than they should. By this, I mean that you get one class to do something that is probably best encapsulated in another, and then you can use delegation to do the same.

+4
source

All Articles