- Mastering Objectoriented Python
- Steven F. Lott
- 297字
- 2021-11-12 16:25:12
Eagerly computed properties
The following is a subclass of Hand
, where total
is a simple attribute that's computed eagerly as each card is added:
class Hand_Eager(Hand): def __init__( self, dealer_card, *cards ): self.dealer_card= dealer_card self.total= 0 self._delta_soft= 0 self._hard_total= 0 self._cards= list() for c in cards: self.card = c @property def card( self ): return self._cards @card.setter def card( self, aCard ): self._cards.append(aCard) self._delta_soft = max(aCard.soft-aCard.hard, self._delta_soft) self._hard_total += aCard.hard self._set_total() @card.deleter def card( self ): removed= self._cards.pop(-1) self._hard_total -= removed.hard # Issue: was this the only ace? self._delta_soft = max( c.soft-c.hard for c in self._cards ) self._set_total() def _set_total( self ): if self._hard_total+self._delta_soft <= 21: self.total= self._hard_total+self._delta_soft else: self.total= self._hard_total
In this case, each time a card is added, the total
attribute is updated.
The other card
property—the deleter—eagerly updates the total
attribute whenever a card is removed. We'll take a look at the deleter in detail in the next section.
A client sees the same syntax between these two subclasses (Hand_Lazy()
and Hand_Eager()
) of Hand
:
d= Deck() h1= Hand_Lazy( d.pop(), d.pop(), d.pop() ) print( h1.total ) h2= Hand_Eager( d.pop(), d.pop(), d.pop() ) print( h2.total )
In both cases, the client software simply uses the total
attribute.
The advantage of using properties is that the syntax doesn't have to change when the implementation changes. We can make a similar claim for getter/setter method functions. However, getter/setter method functions involve extra syntax that isn't very helpful nor informative. The following are two examples, one of which is using a setter method and the other that is using the assignment operator:
obj.set_something(value) obj.something = value
The presence of the assignment operator (=
) makes the intent very plain. Many programmers find it clearer to look for assignment statements than to look for setter method functions.