"Paul Rubin" <"http://phr.cx"@NOSPAM.invalid> wrote in message news:[EMAIL PROTECTED] | philly_bob <[EMAIL PROTECTED]> writes: | > algs=['AlgA', 'AlgB', 'AlgC', 'AlgD', 'AlgE'] | > accs=[] | > for alg in algs: | > thisacc=getattr(alg.accuracy)() |> # picked this technique on comp.lang.python last month
I don't believe that you actually ran this. What you should have picked up for the above was something like globals()[alg].accuracy(). What you probably saw was something like getattr(ob_with_call_attr, 'callable_name')(). But a list of alg objects instead of names, as Paul suggested, is almost always better. | > accs.append(thisacc) | I think what you mean is (untested): | | algs = [AlgA, AlgB, AlgC, AlgD, AlgE] | accs = sorted(((alg.accuracy(), alg.name()) for alg in algs), reverse=True) Use alg.__name__ instead of alg.name(). | for i,(alg_acc, alg_name) in enumerate(accs): | print '%2d %5s %0.2f'% (i, alg_name, alg_acc) | | This assumes a method alg.name() which tells you the name of the algorithm. See above. In 3.0, function names are also .__name__ instead of .func_name, so one can mix callables in a list and get their definitions names uniformly. | I don't understand how you're converting the string 'AlgA' | to an algorithm object in your example above. He was trying to convert name to method with the buggy getattr call. tjr -- http://mail.python.org/mailman/listinfo/python-list