Python从父级而不是子级调用方法

时间:2018-12-09 01:20:23

标签: python python-3.x inheritance abstract-class abstract-methods

给出包含抽象方法的基类:

class AbstractDataSource(object):
    def __curateDataframe(self,dataFrame: pd.DataFrame):
            notFoundColums = set(dataFrame.columns) - set (self.__acceptedHeaders)
            for column in notFoundColums:
                dataFrame.drop(column, axis=1, inplace=True)
            return dataFrame

    def Data(self):
        df = self.__curateDataframe(self._dataFrame)
        df = self.__computeDerivedIndicators(self._dataFrame)
        df.fillna(0,inplace=True)
        return df

    @abc.abstractmethod
    def __readData(self): pass

现在是Child类:

class FileDataSource(AbstractDataSource):
    def __init__(self, filePath):
        self._location = filePath
        self._dataFrame = self.__readData()
        super().__init__()

    def __curateDataframe(self,dataFrame: pd.DataFrame):
        dataFrame.columns = dataFrame.columns.str.replace(" ","")
        dataFrame['Date'] = dataFrame[['Date', 'Time']].apply(lambda x: ' '.join(x), axis=1).apply(lambda x: pd.to_datetime(x))
        dataFrame.set_index('Date')
        return super(FileDataSource,self).__curateDataframe(dataFrame)

    def __readData(self):
        fileDir = os.path.dirname(os.path.realpath('__file__'))
        filename = os.path.join(fileDir, self._location)
        filename = os.path.abspath(os.path.realpath(filename))
        df = pd.read_csv(filename, sep=',',parse_dates=True)
        return df

从程序的入口点调用时

df =  ds.FileDataSource('file.csv').Data()

我的预期结果是使用Data方法调用子FileDataSource方法__curateDataframe

现在我的问题是为什么这种情况不会发生,以及如何实现呢?

P.S。我来自C ++ / C#世界,对继承的工作方式仍然不太熟悉

0 个答案:

没有答案