【发布时间】:2019-03-22 20:35:31
【问题描述】:
我正在编写一个引擎来创建扑克牌,我希望每手牌都只包含独特的牌,即使我从多个牌组中抽牌
我的问题是,这段代码
for z in range(dr):
if self.cards[-1] not in drawcards:
drawcards[z] = self.cards.pop()
不会将花色为 x 且值为 y 的牌注册为与另一张花色为 x 且值为 y 的牌相等
这是我的卡片类:
class Card:
"""A class containing the value and suit for each card"""
def __init__ (self, value, suit):
self.value = value
self.suit = suit
self.vname = value_names[value]
self.sname = suit_names[suit]
def __str__(self):
#Irrelevant
def __repr__(self):
#Irrelevant
如何让我的程序注册花色 x 和值 y 的卡 a 等于花色 x 和值 y 的卡 b?
编辑:
对于以后看这个问题的人,除了__eq__,
def __hash__(self):
return hash((self.value, self.suit))
对于 for 循环中指定的相等性是必需的
【问题讨论】: