我有这三个 Python 类:
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))
但是当我运行这段代码时:
deck1 = Deck()
deck1.populate()
hand1 = Hand()
print(hand1)
打印一整副卡片。该类Hand
似乎正在运行populate(self)
。为什么?