473,395 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 3482
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Roy Smith | last post by:
I've got a function which gets passed a call-back function as a parameter. I want to check to make sure the thing passed in is indeed callable. Is: assert '__call__' in dir (param) the...
7
by: Duncan Smith | last post by:
I recently started to rewrite a class and decided to take a bunch of related methods and put them in a separate callable class. (They're all what I would call pointwise operations on pairs of...
4
by: Michal Vitecek | last post by:
hello everyone, today i've come upon a strange exception, consider the following file test.py: --- beginning of test.py --- class A(object): def method1(parA): print "in A.method1()"
28
by: The Eternal Squire | last post by:
PEP: 336 Title: Make None Callable Version: $Revision: 1.1 $ Last-Modified: $Date: 2004/11/03 16:58:30 $ Author: Andrew McClelland Status: Draft Type: Standards Track Content-Type: text/plain...
3
by: Apple | last post by:
Hi I am a bit new to python. I was wondering if there is a way to determine whether or not a given string is a member method of a given object: def is_a_method(self, attr_name): 'returns True...
5
by: David Rasmussen | last post by:
If I have a string that contains the name of a function, can I call it? As in: def someFunction(): print "Hello" s = "someFunction" s() # I know this is wrong, but you get the idea... ...
10
by: Nicolas Fleury | last post by:
Hi everyone, I was wondering if it would make sense to make staticmethod objects callable, so that the following code would work: class A: @staticmethod def foo(): pass bar = foo() I...
0
by: Steven Bethard | last post by:
Steven Bethard wrote: > (For anyone else out there reading who doesn't already know this, > Steven D'Aprano's comments are easily explained by noting that the > __get__ method of staticmethod...
6
by: Ron Garret | last post by:
If I do this: def f(self): print self class c1: pass setattr(c1, 'm1', f) Then f is automagically transmogrified into the appropriate sort of method depending on how it is used:
6
by: netimen | last post by:
Can I substitute a method of a class by a callable object (not a function)? I can very easy insert my function in a class as a method, but an object - can't. I have the following: class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.