FAQ
Hi there,

How can I turn a string into a callable object/function?

I have a = 'len', and I want to do: if callable(eval(a)): print
"callable", but that doesn't quite work the way I want. :)

Regards,

Guillermo

Search Discussions

  • Bruno Desthuilliers at Apr 21, 2008 at 11:26 am

    Guillermo a ?crit :
    Hi there,

    How can I turn a string into a callable object/function?
    Depends on what's in your string.
    I have a = 'len', and I want to do: if callable(eval(a)): print
    "callable", but that doesn't quite work the way I want. :)
    Works here:
    Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
    [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    a = "len"
    callable(eval(a))
    True
    >>>
  • TimeHorse at Apr 21, 2008 at 11:37 am

    On Apr 21, 7:05 am, Guillermo wrote:
    Hi there,

    How can I turn a string into a callable object/function?

    I have a = 'len', and I want to do: if callable(eval(a)): print
    "callable", but that doesn't quite work the way I want. :)

    Regards,

    Guillermo
    What version of Python are you using? I just tried
    "callable(eval('len'))" on Python 2.5.1 and got True, and eval('len')
    returns <built-in function len>.
  • Guillermo at Apr 21, 2008 at 11:57 am
    This must be the dumbest question ever...

    Solved.
    On Apr 21, 1:05 pm, Guillermo wrote:
    Hi there,

    How can I turn a string into a callable object/function?

    I have a = 'len', and I want to do: if callable(eval(a)): print
    "callable", but that doesn't quite work the way I want. :)

    Regards,

    Guillermo

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 21, '08 at 11:05a
activeApr 21, '08 at 11:57a
posts4
users3
websitepython.org

People

Translate

site design / logo © 2021 Grokbase