I'm writing an engine that creates poker hands, and I'd like each hand to contain only unique cards even though I'm drawing from multiple decks
My problem is, this code
for z in range(dr):
if self.cards[-1] not in drawcards:
drawcards[z] = self.cards.pop()
does not register a card with suit x and value y as being equal to another card with suit x and value y
this is my card class:
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
how can I make my program register card a with suit x and value y as equal to card b with suit x and value y?
Edit:
For people looking at this question in the future, in addition to __eq__
,
def __hash__(self):
return hash((self.value, self.suit))
is necessary for the equality specified in the for loop to work