0

I'm a little confused as to how to directly inherit variables from a parent class' __init__ function. For instance, I have the following:

class BaselineModels:
    def __init__(self):
        self.logpath = './log/models/'
        self.mpath = './models/'

I then create a subclass which has its own __init__ and calls super(), but I can't seem to access self.mpath. I know it's something to do with self being bound to the class instance, but how would I go about achieving this functionality as I have a good number of subclasses for which I don't want to replicate these path variables.

The reason I want this is that I call functions from this parent class from within my subclass which uses the parent's class self variables (csv_to_df is a member of the parent class):

def csv_to_df(self) -> tuple:
        """Reads in CSV file declared in __init__ (self.rpath) and converts it to a number of Pandas DataFrames.

        Returns:
            tuple: Returns tuple of Pandas DataFrames; user features, item features and 
                interactions between items.

        """

        df = pd.read_csv(self.rpath, sep='\t')
        return df

Calling this from the subclass results in

Traceback (most recent call last):
  File "model_baselines.py", line 480, in <module>
    als.run()
  File "model_baselines.py", line 366, in run
    df = self.csv_to_df()
  File "model_baselines.py", line 46, in csv_to_df
    df = pd.read_csv(self.rpath, sep='\t')
AttributeError: 'ALS' object has no attribute 'rpath'

Subclass definition

class ALS(BaselineModels):
    def __init__(self):
        super()
        self.model_name = 'als'

    def run(self):
        df = self.csv_to_df()

I call als.run() from the bottom the file.

EDIT: Updated to include subclass definition

5
  • 1
    Strange, the way you describe your subclass, I would expect it to be able to access mpath. Please provide a minimal reproducible example that includes the subclass' definition. Commented Apr 8, 2019 at 12:13
  • @Kevin I've updated my question to include the subclass definition and some other information. Commented Apr 8, 2019 at 12:16
  • mpath vs rpath? Commented Apr 8, 2019 at 12:19
  • it looks like the AttributeError is on df = pd.read_csv(self.rpath, sep='\t') which is calling self.rpath which as far as I can tell is not set anywhere, do you get a similar error for self.mpath ? Commented Apr 8, 2019 at 12:20
  • @kuomi You can inherit the function using super function. Like super(<base class name>, self).__init__() this way Commented Apr 8, 2019 at 12:20

2 Answers 2

3

You have to call the __init__() method of the super() object. Something like this:

class BaselineModels:
    def __init__(self):
        self.logpath = './log/models/'
        self.mpath = './models/'

class ALS(BaselineModels):
    def __init__(self):
        super().__init__()
        self.model_name = 'als'

    def run(self):
        df = self.csv_to_df()


als = ALS()
als.mpath
# returns:
'./models/'
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I thought super() took care of the initialisation here. This works. Thanks.
1

You can follow this method also.

class BaselineModels:
    def __init__(self):
        self.logpath = './log/models/'
        self.mpath = './models/'

class ALS(BaselineModels):
    def __init__(self):
        super(ALS, self).__init__()
        self.model_name = 'als'

    def run(self):
        df = self.csv_to_df()


als = ALS()
als.mpath
# returns:
'./models/'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.