I'm only just starting to actually dabble seriously in Python, so it's possible this has a very easy answer, but at the moment I'm just dumbfounded. Here's the gist of what my code is:
class deck:
cards = [
'ace of spades',
'two of spades',
'three of spades',
] #...and so on
def deal(self, hand): #takes the last card in the deck and transfers it to the supplied hand
hand.add_card(self.cards.pop(-1))
class hand:
cards = []
def add_card(self, card):
self.cards.append(card)
foobar = deck()
hand1 = hand()
hand2 = hand()
foobar.deal(hand1)
print(hand2.cards) #['three of spades']
What I want is to be able to deal a card from the deck to a hand, by calling deck.deal(hand)
.
The good news is, the above code does indeed take a card from foobar
and add it to hand1
. The bad news is... it also adds it to hand2
.
This is probably the most confused I've ever been from programming. hand1
and hand2
are quite clearly two distinct variables! So why does dealing a card to one hand also deal the same card to the other hand? I mean, the only time hand2
is even mentioned is when it's being created.