I have a question regarding return
convention in Python. I have a class that has some data
attributes and I have several functions that perform some analysis on the data and then store the results as results
attributes (please see the simplified implementation below). Since, the analysis functions mainly update the results
attribute, my question is what is the best practice in terms of return
statements. Should I avoid updating class attributes inside the function (as in process1
), and just return the data and use that to update the results attribute (as in process2
)?
Thanks, Kamran
class Analysis(object):
def __init__(self, data):
self.data = data
self.results = None
def process1(self):
self.results = [i**2 for i in self.data]
def process2(self):
return [i**2 for i in self.data]
a = Analysis([1, 2, 3])
a.process1()
a.results = a.process2()