Is using type as dictionary keys considered good practice in Python?
<p>I have a lookup table like this:</p>

<pre><code>lookup = {
  "&lt;class 'Minority.mixin'&gt;": report_minority,
  "&lt;class 'Majority.mixin'&gt;": report_majority,
}

def report(o):
  h = lookup[str(type(o))]
  h()
</code></pre>

<p>It looks awkward to me as the <code>key</code> is precariously linked to how <code>type()</code> returns and represents a <code>class</code> in <code>string</code>. If one day Python changes its way to represents <code>class</code> types in <code>string</code>, all the codes like this are broken. So I want to have some advice from pros, is such type of keys considered good or bad? Thanks.</p>

<p>Just remove the <code>str</code> from the lookup and from the dictionary. So</p>

<pre><code>lookup = {
   Minority.mixin : report_minority,
   Majority.mixin : report_majority,
}

def report(o):
    h = lookup[type(o)]
    h()
</code></pre>

<p>This fixes your immediate concern and is fairly reasonable code.  However, it seems like dispatching on the type is exactly what OO is for.  So why not make those varying report functions methods on the <code>o</code> object derived from its type?  Then you could just write:</p>

<pre><code>o.report()
</code></pre>

<p>and the correct variant would be obtained from the class.</p>

python python-3.x dictionary