Opposite of repr() (kind of)

G

Guillermo

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
 
B

Bruno Desthuilliers

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.
 
T

TimeHorse

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>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top