I would like to read class attributes from each class all the way up the class inheritance chain.
Something like the following:
class Base(object):
def smart_word_reader(self):
for word in self.words:
print(word)
class A(Base):
words = ['foo', 'bar']
class B(A):
words = ['baz']
if __name__ == '__main__':
a = A()
a.smart_word_reader() # prints foo, bar as expected
b = B()
b.smart_word_reader() # prints baz - how I can I make it print foo, bar, baz?
Obviously, each words
attribute is overriding the others, as it should. How can I do something similar that will let me read the words
attribute from each class in the inheritance chain?
Is there a better way I can approach this problem?
Bonus points for something that will work with multiple inheritance chains (in a diamond shape with everything inheriting from Base
in the end).