472,122 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

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 3424
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 leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.