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
mpathvsrpath?df = pd.read_csv(self.rpath, sep='\t')which is callingself.rpathwhich as far as I can tell is not set anywhere, do you get a similar error forself.mpath?