Python - Dictionary in class not functioning as expected
<p>I have a very basic class <code>Library</code>, and I initialize it with a passed in dictionary (key book name (string), value book shelf location (int)) with values already entered into it. The code looks like this:</p>

<pre><code>class Library(object):
    def __init__(self, book_table):
        self.book_table = book_table

    def get_location(self, book_name):
        if book_name in self.book_table: # ERROR RIGHT HERE
            return self.book_table[book_name]
        else:
            return "Book Not Found"

libraries = []
libraries.append(Library({"Book1":2, "Book2":9}))
print Library(libraries[0]).get_location("Book1")
</code></pre>

<p>For some reason, I am unable to access data from the dictionary from the get_location method, but I am able to access the dictionary data in the initialize method (and I previously tested the represent method and it worked in there too). This is the error I get:</p>

<pre><code>Traceback (most recent call last):
  File "C:/Users/Owner/Documents/Programming/PyCharm/Book_Locator/Book_Locator.py", line 13, in &lt;module&gt;
    print Library(libraries[0]).get_location("Book1")
  File "C:/Users/Owner/Documents/Programming/PyCharm/Book_Locator/Book_Locator.py", line 6, in get_location
    book_name in self.book_table:
TypeError: argument of type 'Library' is not iterable
</code></pre>

<p>I expected it to print out Book1's location, which is a 2.</p>

<p><code>Library(libraries[0])</code> you call <strong>init</strong> again</p>

<p>This code should works</p>

<pre><code>class Library(object):
    def __init__(self, book_table):
        self.book_table = book_table

    def get_location(self, book_name):
        if book_name in self.book_table:  # ERROR RIGHT HERE
            return self.book_table[book_name]
        else:
            return "Book Not Found"


libraries = []
libraries.append(Library({"Book1": 2, "Book2": 9}))

print libraries[0].get_location("Book1")
</code></pre>

python dictionary