I have a class for which I would like to override a variety of methods such as __str__
, __iter__
or len
. However, the implementation of all of these methods are identical. For example, I might have something like this:
def __len__(self):
return list.__len__(self.do_something())
def __str__(self):
return str(self.do_something())
def __iter__(self):
return list.__iter__(self.do_something())
def items(self):
return (self.do_something()).items()
My idea was to capture the methods or magic methods, and call them after performing do_something
(in a type of wrapper function). Is there a way to do this? Or maybe there are alternative solutions?