Given a class structure like this:
class A:
dependencies = ["x", "y"]
class B(A):
dependencies = ["z"]
class C(A):
dependencies = ["n", "m"]
class D(C):
dependencies = ["o"]
I want to know if it's possible to write a function (preferably living on class A) that does something along these lines:
@classmethod
def get_all_dependencies(cls):
return super().get_all_dependencies() + cls.dependencies
For the above classes, the expected output would be:
>>> A.get_all_dependencies():
["x", "y"]
>>> B.get_all_dependencies():
["x", "y", "z"]
>>> C.get_all_dependencies():
["x", "y", "n", "m"]
>>> D.get_all_dependencies():
["x", "y", "n", "m", "o"]
Obviously the above code doesn't work - it just returns the dependencies
of the class I call it on. I'm not sure how to get it to work recursively across all the classes? (I'm eliding a hasattr
check to ensure the parent class call get_all_dependencies()
.)
Walk the
mro
and grab the dependencies is what I'd do:with
cls.mro()[:-1]
being used to excludeobject
.This returns:
Walk the MRO manually:
If you want to deduplicate dependencies that appear repeatedly in the MRO, you can:
You can certainly do this if you define "get_all_dependencies" on each class and, instead of
cls.dependencies
, you reference the class that you're on (ie.super(B, self).get_all_dependences() + B.dependencies
)