Can string be callable as a method ?

J

Jia Lu

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
 
S

Steven D'Aprano

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,
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top