Python: returning an instance of derived class from a base class method -
assume following situation. there base class holding data , derived class assumes data has additional characteristic allows running algorithm
. base class has algorithm processes data in generic way , returns copy result of processing. now, processing not affect specific characteristic of data , therefore can safely assume returned value should of type of derived class.
my current implementation:
class genericdata: def __init__(self, d): self._d = d def process(self): # process data in way compatible # specialization. then, return instance # of specific derived class. return type(self)(some_processing(self._d)) class specificdata(genericdata): """this class gathering of algorithms make assumptions d.""" def __init__(self, d): super().__init__(d) def algorithm(): # stuff assuming characteristic of data pass
my question is: good/pythonic way of handling such case? answer question either different design pattern or better implementation of pattern used above.
Comments
Post a Comment