By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

Can string be callable as a method ?

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.