compare and select classes by member function - a neat way
<p><strong>Given:</strong> </p>

<ol>
<li>a set of class instances (CL) and a reference instance (r)</li>
<li>the class has several getters, such as 'getA', 'getB', ...</li>
</ol>

<p><strong>Todo:</strong> Find those instances from CL that match 'r' by comparing 'getA', 'getB', ... For good code, only one selection function should be written, and you called by giving different getters as the comparison and selection criteria.</p>

<p>My code looks like this:</p>

<pre><code>def selector(r, cl, cmp_function_name):
  return [i for i in CL if getattr(r, cmp_function_name)() == getattr(i, cmp_function_name)()]
# call it like this:
selector(r, cl, 'getA')
selector(r, cl, 'getB')
...
</code></pre>

<p>But I am not sure this is neat or pythonic. What do you think about it and How would you code it?</p>

<p>Thanks!</p>
