cannot make inheritance work with super in module
本问题已经有最佳答案,请猛点这里访问。
我读过通过
1 2 | from .Dataset import Dataset from .CSVDataset import CSVDataset |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import csv import numpy as np from dataset import Dataset class CSVDataset(Dataset): """ reads dataset from csv file """ def __init__(self, file): Dataset.__init__(self) #super(CSVDataset, self).__init__() reader = csv.reader(open(file, 'r'), delimiter=',') x = list(reader) self.container = np.array(x).astype('double') |
和
1 2 | from dataset import CSVDataset data = CSVDataset('test/data1') |
它只适用于
更新:我得到错误
1 2 3 4 5 6 | >>> data = CSVDataset('test/data1') Traceback (most recent call last): File"<stdin>", line 1, in <module> File"dataset/CSVDataset.py", line 13, in __init__ super(CSVDataset, self).__init__() TypeError: must be type, not classobj |
数据集草案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Dataset(Iterable): """ base class representing dataset API. Remember, dataset is used for learning. """ def __init__(self): self.container = None self.counter = None .... class Iterable: def __iter__(self): self.counter = 0 return self def __next__(self): try: label = self[self.counter] self.counter += 1 return label except: raise StopIteration |
您的
修复,使
1 | class Dataset(object): |
另外,看看在Python中旧样式类和新样式类的区别是什么?