Home > Software engineering >  Better to save method result as class variable or return this variable and use as input in another m
Better to save method result as class variable or return this variable and use as input in another m

Time:09-08

I don't have any formal training in programming, but I routinely come across this question when I am making classes and running individual methods of that class in sequence. What is better: save results as class variables or return them and use them as inputs to subsequent method calls. For example, here is a class where the the variables are returned and used as inputs:

class ProcessData:

 def __init__(self):
  pass

 def get_data(self,path):
  data = pd.read_csv(f"{path}/data.csv"}
  return data

 def clean_data(self, data)
  data.set_index("timestamp", inplace=True)
  data.drop_duplicates(inplace=True)
  return data

def main():
 processor = ProcessData()
 temp = processor.get_data("path/to/data")
 processed_data = processor.clean_data(temp)

And here is an example where the results are saved/used to update the class variable:

class ProcessData:

 def __init__(self):
  self.data = None

 def get_data(self,path):
  data = pd.read_csv(f"{path}/data.csv"}
  self.data = data

 def clean_data(self)
  self.data.set_index("timestamp", inplace=True)
  self.data.drop_duplicates(inplace=True)
  
def main():
 processor = ProcessData()
 processor.get_data("path/to/data")
 processor.clean_data()

I have a suspicion that the latter method is better, but I could also see instances where the former might have its advantages. I am sure the answer to my question is "it depends", but I am curious in general, what are the best practices?

CodePudding user response:

Sketch the class based on usage, then create it

Instead of inventing classes to make your high level coding easier, tap your heels together and write the high-level code as if the classes already existed. Then create the classes with the methods and behavior that exactly fits what you need.

PEP AS AN EXAMPLE

If you look at several peps, you'll notice that the rationale or motivation is given before the details. The rationale and motivation shows how the new Python feature is going to solve a problem and how it is going to be used sometimes with code examples.

Example from PEP 289 – Generator Expressions:

Generator expressions are especially useful with functions like sum(), min(), and max() that reduce an iterable input to a single value:

max(len(line)  for line in file  if line.strip())

Generator expressions also address some examples of functionals coded with lambda:

reduce(lambda s, a: s   a.myattr, data, 0)
reduce(lambda s, a: s   a[3], data, 0)

These simplify to:

sum(a.myattr for a in data)
sum(a[3] for a in data)

My methodology given above is the same as describing the motivation and rationale for a class in terms of use. Because you are writing the code that is actually going to use it first.

  • Related