title: How to have different objects from different classes in one dict in Python?
body: <p>I had a question about dictionaries with custom objects. In a dict, I know that the key has to be immutable, so if I want to use a custom class, I have to define its hash function. The hash doc in python recommends you use the hash function on the tuple of the equal dunder method. So for example, i defined the custom class temp as such:</p>
  <pre class="lang-py prettyprint-override"><code>class temp():
      def __init__(self,value):
          self.value = value
      def __hash__(self):
          return hash(self.value)
      def __eq__(self,other):
          return self.value == other.value
      def __lt__(self,other):
          return self.value &lt; other.value
  </code></pre>
  <p>This way I can have they key:value pair such as temp(1):1. So to my question. In python, you can have different types in the same dict. So I declared this dict:</p>
  <pre class="lang-py prettyprint-override"><code>myDict={ temp(1):1, temp(2):2, 'a':1,1:1, (1,2):1, True:1 }
  </code></pre>
  <p>The problem I am facing is that I would get an error for the int:int and bool:int pairing telling me the error:</p>
  <p>'bool' object has no attribute 'value'</p>
  <p>or</p>
  <p>'int' object has no attribute 'value'</p>
  <p>Can someone explain to me why this is the case? The same issue would happen if I have a different class in the dict as well. So an object from a cars class would give this error:</p>
  <p>'cars' object has no attribute 'value'</p>
  <p>Strangely enough in my tests, I found that if the key is a tuple or a float, it works fine.
  Any help would be greatly appreciated. I wanted to know why the error is happening and how I can fix it. MY main goal is to learn how to my one dict that has various objects from different classes.</p>