I've been looking at the following code and am wondering if someone can clarify why the programmer made __wasActive a private field with a getter method? It is simply because when writing code outside this file, the programmer wanted the private fields to be accessible through a method wasActive() only and not directly by writing for example Cell.wasActive ? Thanks!
class Cell(object):
def __init__(self, column, index):
self.segments = []
self.column = column
self.index = index
self.isActive = False
self.__wasActive = False #Why private field?
self.isPredicting = False
self.__wasPredicted = False
self.isLearning = False
self.__wasLearning = False
@property
def wasActive(self):
return self.__wasActive
@property
def wasLearning(self):
return self.__wasLearning
@property
def wasPredicted(self):
return self.__wasPredicted
The reason for "private" members in a first place is always (almost) encapsulation. In this case only a Cell
class can update its __wasActive
field and so an author can sleep well without worrying that somebody else could "mess up" his class's state. Perhaps that field's value is updated when something else is changing. But we do not know this without looking at a relevant code snippet that you seem to not have provided. The getter is there, however, to let "others" read that value at any time, free of charge.