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 
3
  • Your version with super() looks correct to me. What error message do you get? Commented Jan 31, 2015 at 13:22
  • Can you please include the definition of class Dataset too? Presumably this is Python 2? Commented Jan 31, 2015 at 13:30
  • Right, the exception tells you that Dataset is an old-style class, not a new-style unified type. Commented Jan 31, 2015 at 13:32

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.