So I'm trying to compare a Deck object with the evaluated representation of a Deck object and getting
Traceback (most recent call last):
File "C:/Users/Philipp/PycharmProjects/fnaround/src.py", line 3, in <module>
print(Deck() == eval(repr(Deck())))
File "<string>", line 1, in <module>
NameError: name 'Card' is not defined
I can not figure out what it is as I also have overridden the __repr__
method in other classes and it works fine. I think it has something to do with it jumping from the Deck class to the Card class but I'm not sure. Can someone explain to me how the program is moving through the classes and how to fix the error. Thanks
class Deck:
suits = ['\u2660', '\u2661', '\u2662', '\u2663']
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
def __init__(self):
self.deck = []
for suit in self.suits:
for rank in self.ranks:
self.deck.append(Card(rank, suit))
def __repr__(self):
return 'Deck({})'.format(self.deck)
def __eq__(self, other):
return self.deck == other.deck
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __repr__(self):
return "Card('{}', '{}')".format(self.rank, self.suit)
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
print(Deck() == eval(repr(Deck())))