mercredi 1 août 2018

Function encapsulated in class to load required files just once

I have got an algorithm that has got one function calculate(x, y) which calls many smaller functions which require to use two big files that I want to load only once.

One way to load it once is to load them and pass them to calculate(x, y, f1, f2) and they will be passed to next and next functions that are called inside.

def calculate(x, y, f1, f2):
    a = function1(x, y, f1) 
    b = function2(x, y, f2)

def function1(x, y, f1):
    #process further with f1 that is passed by arg further and further...

Other way is to make a class

class Algo
    def __init__():
         self.f1 = load_f1...
         self.f2 = load_f2...

    def calculate(self, x, y):
        a = function1(x, y) 
        b = function2(x, y)

    def function1(self, x, y):
        #process further with self.f1...

and load files in init and call it this way. Is it proper way to encapsulate function in a class? Is there any pattern that solves that problem?

Aucun commentaire:

Enregistrer un commentaire