从字符串调用函数 [英] calling a function from string
问题描述
i有一个我想调用的函数,但为了使它更多
dynamic我首先构造一个可以等效的字符串到我希望调用的函数的
名称。我怎么能这样做?字符串
可能包含模块的名称。
例如
a_string =''datetime。 ''+''今天()''
怎么能把a_string称为函数?
谢谢
james
i have a function that I could like to call, but to make it more
dynamic I am constructing a string first that could equivalent to the
name of the function I wish to call. how could I do that? the string
could might include name of the module.
for example
a_string = ''datetime.'' + ''today()''
how could I call a_string as function?
Thanks
james
推荐答案
2007年10月22日,james_027< ca ******** @ gmail.comwrote :
On 10/22/07, james_027 <ca********@gmail.comwrote:
i有一个我想打电话的功能,但要做得更多
dynamic我首先构造一个字符串,它可以相当于我希望调用的函数的
名称。我怎么能这样做?字符串
可能包含模块的名称。
例如
a_string =''datetime。 ''+''今天()''
如何将a_string作为函数调用?
i have a function that I could like to call, but to make it more
dynamic I am constructing a string first that could equivalent to the
name of the function I wish to call. how could I do that? the string
could might include name of the module.
for example
a_string = ''datetime.'' + ''today()''
how could I call a_string as function?
你可以使用getattr:
function_name =''time''#这是一个字符串
module_name =''time''#这也是一个字符串
my_function = getattr(module_name,function_name)#这是
函数对象,
#等价于my_function = time.time
my_function()#这是函数调用,相当于time.time()
>
再见
francesco
you could use getattr:
function_name = ''time'' # this is a string
module_name = ''time'' # this is a string, too
my_function = getattr(module_name, function_name) # this is the
function object,
# equivalent to my_function = time.time
my_function() # This is the function call, equivalent to time.time()
bye
francesco
我有一个我想打电话的功能,但要多赚一些
i have a function that I could like to call, but to make it more
dynamic我首先构造一个字符串,它可以等同于我希望调用的函数的
名称。我怎么能这样做?字符串
可能包含模块的名称。
例如
a_string =''datetime。 ''+''今天()''
如何将a_string作为函数调用?
dynamic I am constructing a string first that could equivalent to the
name of the function I wish to call. how could I do that? the string
could might include name of the module.
for example
a_string = ''datetime.'' + ''today()''
how could I call a_string as function?
以下列时尚之一使用''eval'':
a_string_1 =''datetime。''+ ''今天''
a_string_2 =''datetime。''+''今天()''
eval(a_string_1)()
eval(a_string_2)
Trent。
Use ''eval'' in one of the following fashions:
a_string_1 = ''datetime.'' + ''today''
a_string_2 = ''datetime.'' + ''today()''
eval(a_string_1)()
eval(a_string_2)
Trent.
Trent Nelson napisa3(a):
Trent Nelson napisa3(a):
>我有一个我想调用的函数,但为了使它更具动态我首先构造一个字符串,它可以等同于
我希望调用的函数的名称。我怎么能这样做?字符串
可能包含模块的名称。
a_string =''datetime。''+''今天()''
如何将a_string作为函数调用?
>i have a function that I could like to call, but to make it more
dynamic I am constructing a string first that could equivalent to the
name of the function I wish to call. how could I do that? the string
could might include name of the module.
for example
a_string = ''datetime.'' + ''today()''
how could I call a_string as function?
以下列时尚之一使用''eval'':
a_string_1 =''datetime。''+ ''今天''
a_string_2 =''datetime。''+''今天()''
eval(a_string_1)()
eval(a_string_2)
Use ''eval'' in one of the following fashions:
a_string_1 = ''datetime.'' + ''today''
a_string_2 = ''datetime.'' + ''today()''
eval(a_string_1)()
eval(a_string_2)
不要使用eval()。它不仅被弃用了,而且还不安全。
-
Jarek Zgoda
Skype:jzgoda | GTalk: zg***@jabber.aster.pl |声音:+48228430101
我们读过Knuth所以你不必这么做。 (蒂姆·彼得斯)
Do not use eval(). Not only it''s deprecated, it''s also unsafe.
--
Jarek Zgoda
Skype: jzgoda | GTalk: zg***@jabber.aster.pl | voice: +48228430101
"We read Knuth so you don''t have to." (Tim Peters)
这篇关于从字符串调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!