我有两个私有变量(属性),我给了它们getter和setter,现在它为每个创建了两个变量(我只想要一个,我希望它是私有的):
_indoor_location_cards
indoor_location_cards
_outdoor_location_cards
outdoor_location_cards
我在调试器中发现了这一点。
这是我的代码:
the_game = None
class Game(object):
"""Template for the Zimp game."""
_indoor_location_cards = []
_outdoor_location_cards = []
game_dev_cards = []
the_user = None
items = []
the_time = None
def __init__(self):
pass
#getter and setter for the private indoor card list
def indoor_location_cards():
doc = """The priavte list for indoor location cards
of the game. Has a getter, setter, & deleter"""
def fget(self):
return self._indoor_location_cards
def fset(self, value):
self._indoor_location_cards = value
def fdel(self):
del self._indoor_location_cards[0:len(self._indoor_location_cards)]
return locals() # credit: David Niergarth
indoor_location_cards = property(**indoor_location_cards())
#getter and setter for the private outdoor card list
def outdoor_location_cards():
doc = """The priavte list for outdoor location cards
of the game. Has a getter, setter, & deleter"""
def fget(self):
return self._outdoor_location_cards
def fset(self, value):
self._outdoor_location_cards = value
def fdel(self):
del self._outdoor_location_cards[0:len(self._outdoor_location_cards)]
return locals() # credit: David Niergarth
outdoor_location_cards = property(**outdoor_location_cards())
def main():
pass
if __name__ == '__main__':
main()
the_game是一个模块范围变量,包含一个Game实例。
我只使用代码来设置像这样的the_games属性:
model.zimp.the_game = model.zimp.Game()
model.zimp.the_game.the_time = datetime.time(9, 0)
# Add the indoor cards to the indoor cards list.
model.zimp.the_game._indoor_location_cards.append(card1)
model.zimp.the_game._indoor_location_cards.append(card2)
model.zimp.the_game._indoor_location_cards.append(card3)
model.zimp.the_game._outdoor_location_cards.append(card9)
model.zimp.the_game._outdoor_location_cards.append(card10)
model.zimp.the_game._outdoor_location_cards.append(card11)