How can I make class properties immutable?
问题 @property is a nice way to define getters. When the property is mutable, the reference returned can be used to modify the property in ways not controlled by the class definition. I'll use a banana stand as a motivating analogy, but this issue applies to any class that wraps a container. class BananaStand: def __init__(self): self._money = 0 self._bananas = ['b1', 'b2'] @property def bananas(self): return self._bananas def buy_bananas(self, money): change = money basket = [] while change >= 1