1

The Zen of python tells us: There should be one and only one obvious way to do it.

This is difficult to put in practice when it comes to the following situation.

A class receives a list of documents. The output is a dictionary per document with a variety of key/value pairs. Every pair depends on a previous calculated one or even from other value/pairs of other dictionary of the list.

This is a very simplified example of such a class.

What is the “obvious” way to go? Every method adds a value/pair to every of the dictionaries.

class T():
    
    def __init__(self,mylist):
        #build list of dicts
        self.J = [{str(i):mylist[i]} for i in range(len(mylist))]
        # enhancement 1: upper
        self.method1()
        # enhancement 2: lower
        self.J = self.static2(self.J)

    def method1(self):
        newdict = []
        for i,mydict in enumerate(self.J):
            mydict['up'] = mydict[str(i)].upper()
            newdict.append(mydict)
        self.J = newdict
    
    @staticmethod
    def static2(alist):
        J = []
        for i,mydict in enumerate(alist):
            mydict['down'] = mydict[str(i)].lower()
            J.append(mydict)
        return J
    
    @property
    def propmethod(self):
        J = []
        for i,mydict in enumerate(self.J):
            mydict['prop'] = mydict[str(i)].title()
            J.append(mydict)
        return J
        
    # more methods extrating info out of every doc in the list
    # ...

self.method1() is simple run and a new key/value pair is added to every dict. The static method 2 can also be used. and also the property.

Out of the three ways I discharge @property because I am not adding another attribute. From the other two which one would you choose?

Remember the class will be composed by tens of this Methode that so not add attributes. Only Update (add keine pair values) dictionaries in a list.

I can not see the difference between method1 and static2.

thx.

JFerro
  • 3,203
  • 7
  • 35
  • 88
  • 1
    `method1` and `static2` are not equivalent. – juanpa.arrivillaga Mar 30 '21 at 17:30
  • Why not? They produce the same result in different ways. – JFerro Mar 30 '21 at 18:26
  • I.e. they add one key/value pair to each dict of the list – JFerro Mar 30 '21 at 18:27
  • Yes, but their signatures are not the same. When you write software, you are creating abstractions and APIs. The APIs of these two methods would be different. Which one is more suitable would be a judgement call you have to make, but they are not the same. – juanpa.arrivillaga Mar 30 '21 at 18:57
  • But I would note, your staticmethod could just be a function. There is, IMO, essentially no good use-cases for staticmethods. Except for maybe organizational purposes, but most of the time, they would be just as good (and better) as module-level functions. – juanpa.arrivillaga Mar 30 '21 at 18:58
  • Ok. Got your point in the last comment. So you would say the "obvious" way to do it is implemented several methods as in methods and forget about static methods. Which actually makes sense since I don't want to access the methods from outside the class. – JFerro Mar 30 '21 at 19:13

0 Answers0