The simplest solution is probably just to create another subclass.
class DB(object):
...
class Transformation(object):
def transform(self, obj):
...
def get(self, id):
return self.transform(super(Transformation, self).get(id))
class Cache(object):
def __init__(self, *args, **kwargs):
self.cache = Cache()
super(Cache, self).__init__(*args, **kwargs)
def get(self, id):
if id in self.cache:
return self.cache.get(id)
else:
self.cache.set(id, super(Cache, self).get(id))
return self.cache.get(id)
class DBwithTransformation(Transformation, DB):
pass
, type .
class DBwithTransformation(Transformation, DB):
pass
db = DBwithTransformation(arg1, arg2, ...)
db = type("DB", (Transformation, DB), {})(arg1, arg2, ...)
, Scala.
- python , (DB). , mixin .
. ; , , ( , object).