A untold story about “composition” and “multiple inheritance” in PYTHON

I extensively tried using multiple inheritance and super keyword in achieving compositional pattern..I tried for days and nothing seem to make sense..Every attempt ended up in deep and darkest rabbit hole.After hurting my brain for months, I was able to address compositional pattern without using “decorator”, and my good god, results = MIND BLOWN????.My reaction when everything made sense.This article will be long winded, boring lecture on composition pattern using just super keyword and multiple inheritance..We clearly don’t want to go that route..I would like to share the code snippet of validation library that we built for validating parsed JSON object in python.Multiple Inheritance on steroidsCheck out the library itself for diving deep into how super keyword can be used in writing program that adheres to the principle of “Separation of Concern”.RobusGauli/jsonvalidateWIP..Contribute to RobusGauli/jsonvalidate development by creating an account on GitHub.github.comCONCLUSION:Use multiple inheritance only when you absolutely understand how base classes during multiple inheritance are linearized using Class Resolution Algorithm.Understand the “method resolution order” and know how you can manipulate linearization during class resolution before abandoning “decorator” for composition.Once you understand how all these delicate parts are managed in python, you will realize super is super and we should absolutely give it a shot..It will hurt your good old brain for a while but the API you can build using this pattern is absolute magic..Cheers ????.????!!. More details

Leave a Reply