How to implement the composition pattern? I have a class Container which has an attribute object Contained. I would like to redirect/allow access to all methods of Contained class from Container by simply calling my_container.some_contained_method(). Am I doing the right thing in the right way?
I use something like:
class Container:
def __init__(self):
self.contained = Contained()
def __getattr__(self, item):
if item in self.__dict__: # some overridden
return self.__dict__[item]
else:
return self.contained.__getattr__(item) # redirection
Background:
I am trying to build a class (Indicator) that adds to the functionality of an existing class (pandas.DataFrame). Indicator will have all the methods of DataFrame. I could use inheritance, but I am following the "favor composition over inheritance" advice (see, e.g., the answers in: python: inheriting or composition). One reason not to inherit is because the base class is not serializable and I need to serialize.
I have found this, but I am not sure if it fits my needs.