Converting a strng to an anonymous function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Seese

    Converting a strng to an anonymous function

    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.
  • bearophileHUGS@lycos.com

    #2
    Re: Converting a strng to an anonymous function

    On Sep 29, 11:25 pm, Nathan Seese <uninver...@lav abit.comwrote:
    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

    Comment

    • Nathan Seese

      #3
      Re: Converting a strng to an anonymous function

      On Sep 29, 11:25 pm, Nathan Seese <uninver...@lav abit.comwrote:
      >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
      Thanks, that works great!

      Comment

      Working...