1

I have these three Python classes:

class Card(object):
    RANKS = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
    SUITS = ["c", "d", "h", "s"]
    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

    def __str__(self):
        return self.rank + self.suit

    def __lt__(self, other):
        return self.value < other.value

    def __gt__(self, other):
        return self.value > other.value

    @property
    def value(self):
        return RANKS.index(self.rank) + SUITS.index(self.suit)/4


class Hand(object):
    def __init__(self, cards = []):
        self.cards = cards
        self.tricks = 0

    def __str__(self):
        return " ".join([str(card) for card in self.cards])

    def add(self, card):
        self.cards.append(card)

    def remove(self, card):
        self.cards.remove(card)


class Deck(Hand):
    def populate(self):
        for rank in Card.RANKS:
            for suit in Card.SUITS:
                self.add(Card(rank, suit))

But when I run this code:

deck1 = Deck()
deck1.populate()
hand1 = Hand()
print(hand1)

A whole deck of cards print. The Hand class appears to be running populate(self). Why?

LazySloth13
  • 2,369
  • 8
  • 28
  • 37

1 Answers1

1

Your problem is here:

def __init__(self, cards = []):
        self.cards = cards
        self.tricks = 0

You see, in Python's function definition like def __init__(self, cards=[]): the default parameters are evaluated only once, when the function definition is loaded by interpreter, so they behave sort of global-ish. Also note that the list is a mutable object, so, by definition, it can change it's elements. In result, when You call self.cards.append(card), it appends to this once evaluated, every time the same, list. The solution is:

def __init__(self, cards=None):
    if cards is None:
         cards = []
    self.cards = cards
    self.tricks = 0
Wikiii122
  • 486
  • 3
  • 10