Is it possible to set a property with shorthand operators like +=, or -=?
I've read this post, What's the pythonic way to use getters and setters?, but it deals mainly with direct assignment in terms of object.attr = value
. When thinking about it, I'm finding it difficult to be possible and I've never used setters so I'm not sure.
It is within the Player.hit()
method where I'm attempting to achieve this where I'm subtracting and integer reduced_points
from another integer value
property:
self.hands[x].value -= reduced_points
NOTE: Hand.value
is an @property
due to that I need to dynamically add the values of each card in case a card is added to that hand.
class Card:
def __init__(self, pip, suit, *args, **kwargs):
self.pip = pip
self.suit = suit
if kwargs and 'face_card' in kwargs:
self.face_card = kwargs['face_card']
class Hand:
def __init__(self, cards):
self.cards = cards # [Card(), Card(), Card()]
self._value = sum(card.pip for card in cards)
@property
def soft(self):
return any(
hasattr(card, 'face_card') and
card.face_card == "Ace" for card in self.cards
)
@property
def value(self):
return sum([card.pip for card in self.cards])
@value.setter
def set_value(self, value):
new_value = self._value - value
self._value = new_value
class Player:
def __init__(self):
self.name = "Player"
self.hands = []
self.chips = 0
self._bet = 0
def hit(self, hand):
if hand.soft:
x, hand = list(filter(
lambda h: h[1].soft, enumerate(self.hands)
))[0]
total_aces = len(
list(
filter(lambda c: hasattr(c, 'face_card') and
c.face_card == "Ace", self.hands[x].cards)
)
)
reduced_points = 10 * (total_aces - 1)
self.hands[x].value -= reduced_points
if self.hands[x].value > 21:
return self.hands[x], "BUST"
if hand.value > 21:
return hand , "BUST"
return hand, "HIT"