I am messing around building a deck of cards. I have an issue where I have read that eval(repr(object)) should be equivalent to the object stack overflow topic.
I implemented two classes for my cards, a Card class and a Deck class. Each has a repr method in which I attempted to keep as close to the syntax of actually creating the object. Code for init and repr of Card and Deck:
class Card(object):
def __init__(self, num, suit):
self.__num = num
self.__suit = suit
def __repr__(self):
return "{!r}({!r},{!r})".format(self.__class__, self.__num, self.__suit)
class Deck(object):
def __init__(self, cards=None):
if not cards:
self.__cards = {"diamonds":[], "hearts":[], "spades":[], "clubs":[]}
else:
self.__cards = cards
def __repr__(self):
return "{!r}({!r})".format(self.__class__, self.__dict__)
and here is the error:
print(eval(repr(self.deck.deck)))
File "<string>", line 1
{'hearts': [<class 'cards.Card'>(1,'hearts'), <class 'cards.Card'>(2,'hearts'), <class 'cards.Card'>(3,'hearts'), <class 'cards.Card'>(4,'hearts'), <class 'cards.Card'>(5,'hearts')], 'clubs': [<class 'cards.Card'>(1,'clubs'), <class 'cards.Card'>(2,'clubs'), <class 'cards.Card'>(3,'clubs'), <class 'cards.Card'>(4,'clubs'), <class 'cards.Card'>(5,'clubs')], 'spades': [<class 'cards.Card'>(1,'spades'), <class 'cards.Card'>(2,'spades'), <class 'cards.Card'>(3,'spades'), <class 'cards.Card'>(4,'spades'), <class 'cards.Card'>(5,'spades')], 'diamonds': [<class 'cards.Card'>(1,'diamonds'), <class 'cards.Card'>(2,'diamonds'), <class 'cards.Card'>(3,'diamonds'), <class 'cards.Card'>(4,'diamonds'), <class 'cards.Card'>(5,'diamonds')]}
^
SyntaxError: invalid syntax
I am simply guessing that it is because of how the Card class is represented. I attempted substituting {!r} for {!s} but it didn't change it. I was considering just hard coding the fact that it is of the class Card in there, but I was hoping there would be some specific way I am meant to do it.
To solve:
Just needed to add .__name__
after the self.__class__
and use {!s} (so as to remove the quotes) rather that {!r}