0
我一直在看下面的代碼,並想知道如果有人可以澄清爲什麼程序員使__wasActive私人領域與getter方法?這只是因爲當在這個文件之外編寫代碼時,程序員希望專用字段只能通過wasActive()方法訪問,而不能直接編寫,例如Cell.wasActive?謝謝!Python的私人領域與getter方法
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