0

I have read about new way to call ancestor's constructor via super(..).__init__() and wanted to use it. I have a directory dataset with three files: __init__.py

from .Dataset import Dataset
from .CSVDataset import CSVDataset

CSVDataset.py

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')

and Dataset.py. When I use it from ../dataset like this

from dataset import CSVDataset
data = CSVDataset('test/data1')

it works only with Dataset.__init__(self). It should work with super(CSVDataset, self).__init__() but it does not. Why is that?

update: I get error

>>> 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

Dataset draft:

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
lord.didger
  • 1,357
  • 1
  • 17
  • 31

1 Answers1

0

Your Dataset class is a Python 2.x old style class. super() only works on new style classes, where the class hierarchy ultimately inherits from object.

To fix, make Dataset inherit from object:

class Dataset(object):

Also see What is the difference between old style and new style classes in Python?

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343