Given:
- a set of class instances (CL) and a reference instance (r)
- the class has several getters, such as 'getA', 'getB', ...
Todo: 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.
My code looks like this:
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')
...
But I am not sure this is neat or pythonic. What do you think about it and How would you code it?
Thanks!