I have a class with one method which outputs a dict
. The class needs a file_path to be instantiated. The method needs a string parameter to output the dict. Here is some pseudo-code.
class A():
def __init__(self, file_path):
self.file_path = file_path
def create_dict(self, param):
#uses self.filepath
#creates a dict called data. The dict contains 2 keys and each key contains a list. The first list is a list of strings and the second a list of integers
return data
The procedure to generate this dict
is irrelevant in my opinion. The final output is as follows:
data = {'Key1': ['String1', 'String2', 'String3',....], 'Key2': [1,2,4,....]}
When I started to implement this class I noticed that I wanted to have data
be an object instead. This object would have methods data.printKey1()
and data.printKey2()
which would output the following:
>>> data.printKey1()
String1
String2
String3
...
>>>data.printKey2()
1
2
4
Now I'm stuck because I really don't want to reconstruct the whole procedure by which data
is generated, but I need it to be an object.
I am not sure whether I should use inheritance, class containment or composition (or some other construct I am unaware of). In theory, what is the best practice to use here? How can I generate this new data
class/object without having to rework the entire procedure contained in the create_dict
method?