comparing object with repr() is throwing NameError
<p>So I'm trying to compare a Deck object with the evaluated representation of a Deck object and getting</p>

<pre><code>Traceback (most recent call last):
  File "C:/Users/Philipp/PycharmProjects/fnaround/src.py", line 3, in &lt;module&gt;
    print(Deck() == eval(repr(Deck())))
  File "&lt;string&gt;", line 1, in &lt;module&gt;
NameError: name 'Card' is not defined
</code></pre>

<p>I can not figure out what it is as I also have overridden the <code>__repr__</code> 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 </p>

<pre><code>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())))
</code></pre>

<p>The current issue (causing the <code>NameError</code>) is that when you're running the <code>eval</code> on a <code>Deck</code> instance's <code>repr</code>, you don't have the name <code>Card</code> in the local namespace. This means that When <code>eval</code> tries to interpret the string that <code>repr(Deck())</code> returns, it fails when it comes across the first <code>Card</code>.</p>

<p>You can fix this part of the problem simply by importing <code>Card</code> as well as <code>Deck</code> into the namespace where you're running the other code:</p>

<pre><code>from deck_module import Deck  # you've not said what your module names are,
from card_module import Card  # so I'm making these up

eval(repr(Deck())) # raises a TypeError, but not a ValueError any more
</code></pre>

<p>Adding the <code>import</code> of the <code>Card</code> class doesn't make the code work (due to shortcomings of the <code>Deck</code> constructor), but at least it will get you past the <code>NameError</code>.</p>

python class eval repr