469,285 Members | 2,561 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,285 developers. It's quick & easy.

Can string be callable as a method ?

Hi all
I tried to scan a directory and __import__ all modules ,
<log>
imported module: help
imported module: __init__
imported module: hi
imported module: thanks

and I scaned all methods in them, and put them to a list like:

[['f_chelp', 'f_help'], [], ['f_exclaim', 'f_hi', 'random'],
['f_thanks', 'random']]

But how can I call them from that list??

Thank you.

Jia Lu

May 28 '07 #1
1 3334
On Mon, 28 May 2007 06:45:47 -0700, Jia Lu wrote:
Hi all
I tried to scan a directory and __import__ all modules ,
<log>
imported module: help
imported module: __init__
imported module: hi
imported module: thanks

and I scaned all methods in them, and put them to a list like:

[['f_chelp', 'f_help'], [], ['f_exclaim', 'f_hi', 'random'],
['f_thanks', 'random']]

But how can I call them from that list??
>>import math
dir(math)
['__doc__', '__file__', '__name__', 'acos', 'asin', ... ]
>>>
math.__dict__['acos'](0)
1.5707963267948966
>>getattr(math, 'asin')(0)
0.0

Instead of storing the names of the functions, better to store the
functions themselves:
>>my_list = [math.acos, math.asin]
my_list[0](1)
0.0
Hope this helps,
--
Steven.

May 28 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Roy Smith | last post: by
7 posts views Thread by Duncan Smith | last post: by
4 posts views Thread by Michal Vitecek | last post: by
28 posts views Thread by The Eternal Squire | last post: by
5 posts views Thread by David Rasmussen | last post: by
10 posts views Thread by Nicolas Fleury | last post: by
reply views Thread by Steven Bethard | last post: by
6 posts views Thread by netimen | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.