0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
user32882
  • 5,094
  • 5
  • 43
  • 82
  • first - watch this video (by Python core developer): https://www.youtube.com/watch?v=o9pEzgHorH0 - your class is not class. Afterwards, if any more questions - welcome back. – Drako Jun 01 '18 at 11:14
  • 2
    @Drako and ? How is this supposed to answer the question ? – bruno desthuilliers Jun 01 '18 at 11:22
  • @brunodesthuilliers I expected question to disappear after seeing that video, but may be replaced with different one that would be more appropriate for explanatory answer – Drako Jun 01 '18 at 12:04
  • @Drako the point of the video you posted is to make peoples coming from Java or similar languages realize they don't _necessarily_ need classes everywhere and think twice before writing a class that may - or not - be overkill. Like all "golden rules", it's not a rule, it' a general guideline. Like all "golden rules" too, it's to be taken with a (huge) grain of salt, and actually to be totally ignored once you understand the reasons behind - at this point you're the only one who knows what makes sense for the problem at hand. IOW: don't be a purist. – bruno desthuilliers Jun 01 '18 at 12:30
  • @brunodesthuilliers sorry just also teaching Python at part time and would be difficult to work and advice different from what I teach - even I know that in real world sometimes ugly solution might be necessary in certain cases - had to do it many times myself :) – Drako Jun 01 '18 at 12:47
  • @Drako why "ugly" ? How is a class-based decorator (basically 'a class with an init and one method') "uglier" than a closure-based one ? Have you ever looked at the `Method` type implementation ? (hint: basically another 'class with an init and one method' ). Sorry but you are STILL being dogmatic. – bruno desthuilliers Jun 01 '18 at 13:17

1 Answers1

1

Do you really need a class for this ? a simple function would work:

def printkey(data, key):
    for line in data[key]:
        print(line)

printkey(data, "key1")
printkey(data, "key2")

Actually, mixing domain logic with presentation is a well known antipattern. If you really want a class (to make some narrow-minded OO purist happy or for better reasons that are not explained in your question), then you can of course write some "presenter" class:

class DataPrinter(object):
    def __init__(self, data):
        self.data = data

    def _printkey(self, key):
        for line in self.data[key]:
            print(line)

    def printkey1(self):
        self._printkey("key1")

    def printkey2(self):
        self._printkey("key2")

but that's really mostly useless IMHO, at least for the given example (it might make sense for some much more complex data model and presentation stuff).

Oh and yes, FWIW, Python functions ARE objects (instances of class function) so you can just tell some-narrow-minded-OO-purist that the function-based solution IS actually proper OO design too xD (poor Java dudes had to invent a design pattern - the "functor" - to hide the fact that sometimes all you need is a plain old function...).

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • :) your answer is somewhat the same as info in that video that I put as mandatory before re-asking question :) - "when you see class with 2 methods, one of them is __init__ - you know - it's not a class, it's method :D – Drako Jun 01 '18 at 12:01
  • @Drako that doesn't mean you should not use a custom class either - it all depends on the context, there's NO golden rule. – bruno desthuilliers Jun 01 '18 at 12:26