Say I got a simple class with one public and one private method. If I call the private method in the public method – should it return a value, or should that value be set as a field in my object, for example,
class Test:
def __init__(self, path):
self.path = path
def __getNoOfFiles(self):
'count files in self.path'
return no_of_files
def readDir(self)
...
no_of_files = __getNoOfFiles()
or
class Test:
def __init__(self, path):
self.path = path
self.no_of_files = 0
def __getNoOfFiles(self):
self.no_of_files = 'count files in self.path'
def readDir(self)
__getNoOfFiles()
no_of_files = self.no_of_files
It makes little difference in most circumstances, but there are minor reasons for choosing one way or the other. If the value is likely to be used more than once and it costs something to compute, it may be helpful to cache it in the class instance. But classes often include methods that do nothing but return values from the instance, so the return-value option fits with that style.
In general, whatever looks better to your eye.