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>Take a look at that script, I don't think it's perfect but it works and I think it's more elegant than your solution :</p>

<pre><code>#!/usr/bin/env python3

class Person:
    pass

def display_person():
    print("Person !")

class Car:
    pass

def display_car():
    print("Car !")

lookup = {
    Person: display_person,
    Car: display_car
}


def report(o):
    lookup[o.__class__]()


report(Person())
report(Car())
</code></pre>

<p>Edit : modification for Python3</p>

python python-3.x dictionary