I am building a simple card game using classes and encountering something confusing.
To start with here are the relevant methods of my 'Deck' class.
class Deck(object):
def __init__(self, starting_cards=[]):
self._cards = starting_cards
def get_amount(self):
return len(self._cards)
def add_card(self, card):
self._cards.append(card)
And here is part of my 'Player' class:
class Player(object):
def __init__(self, name):
self._name = name
self._hand = Deck()
self._coders = Deck()
def get_hand(self):
return self._hand
def get_coders(self):
return self._coders
def has_won(self):
if self._coders.get_amount() >= 4:
return True
else:
return False
There are a few 'Card' sub-classes but they are irrelevant to this question, the following code should add 3 cards to self._hand, yet it adds the cards to both self._hand and self._coders, which are two completely different instances of the Deck() class.
player = Player("Lochie Deakin-Sharpe")
player.get_hand().add_card(NumberCard(3))
player.get_hand().add_card(KeyboardKidnapperCard())
player.get_hand().add_card(AllNighterCard())
player.get_hand()
Which part of my code is adding these cards to self._coders, which are far as I can tell is not called?
After running the above code here are some commands:
>>> player.get_coders()
Deck(NumberCard(3), KeyboardKidnapperCard(), AllNighterCard())
>>> player.get_hand().add_card(4)
>>> player.get_hand()
Deck(NumberCard(3), KeyboardKidnapperCard(), AllNighterCard(), 4)
>>> player.get_coders()
Deck(NumberCard(3), KeyboardKidnapperCard(), AllNighterCard(), 4)