On Sep 29, 11:25 pm, Nathan Seese <[EMAIL PROTECTED]> wrote:
> I'm writing a program to sort files with arbitrary python code. The
> method I'm using for that is to pass sort an anonymous function taken
> from the arguments. I'm wondering how to change a raw string into an
> anonyous function.

Is this enough for you?

>>> L = [1, -5, 7, -9]
>>> sorted(L, key=abs)
[1, -5, 7, -9]
>>> func = "abs"
>>> sorted(L, key=eval(func))
[1, -5, 7, -9]
>>> func = "abs(x)"
>>> sorted(L, key=lambda x: eval(func))
[1, -5, 7, -9]

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to