I have different Classes which I need to initialize partially in almost the same way. The init function takes in input a file path and/or some numpy data.
class InertialData1:
def __init__(self, file=None, data=None):
# --- this is the (almost) common part ---
if file is None:
self.file = 'Unknown path to file'
else:
self.file = file
assert isinstance(self.file, str)
if data is None:
self.data = read_logfile(file, 'a_token')
else:
self.data = data
assert isinstance(self.data, np.ndarray)
# --- end of the common part ---
# Here other stuff different from class to class
The token in the read_logfile() function (an external function) also changes from class to class.
class InertialData2:
def __init__(self, file=None, data=None):
# here the common code as above, but with 'another_token' instead of 'a_token'
# here other stuff
...
Writing the same lines in all the classes definition is a no go, of course.
Possible solution.
I thought about defining an external function like this
def initialize(file, data, token):
if file is None:
file = 'Unknown path to file'
else:
file = file
assert isinstance(file, str)
if data is None:
data = read_logfile(file, token)
else:
data = data
assert isinstance(data, np.ndarray)
return file, data
Then I could use it inside the init method of each class like this:
class InertialData1:
def __init__(self, file=None, data=None):
self.file = file
self.data = data
self.file, self.data = initialize(self.file, self.data, token='a_token')
This way seems to be working. I just have the feeling it is not the best way of doing it, or not pythonic, and I hope to learn something from your answers :) Thanks