[Beginner]通过字符串中的名称调用函数 [英] [Beginner] Calling a function by its name in a string
本文介绍了[Beginner]通过字符串中的名称调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好:
Python中是否存在元语言功能(我知道有很多)
调用一个名字在字符串中的函数?
类似于:
__call __(" foo")
而不是:
foo()
问候,
铁托
解决方案
2005年7月27日,Tito< ti ******************* @ gmail.com>写道:大家好:
Python中是否有元语言功能(我知道有很多)来调用一个字符串名称的函数?
类似的东西:
__call __(" foo")
而不是:
foo()
def foo():print" foobarred"
.... foo()
foobarred eval(" foo()")
foobarred
>
Peace
Bill Mill
bill.mill at gmail.com
Tito写道:<你好所有:
Python中是否有元语言功能(我知道有很多)来调用一个名字在字符串中的函数?
类似的东西:
__call __(" foo")
而不是:
foo()
问候,
Tito
eval(''foo()'')应该这样做,但它说是一种不好的做法;)
Tito写道:大家好:
Python中是否有元语言功能(我知道有很多)来调用一个名字的函数在字符串中?
类似于:
__call __(" foo")
而不是:
foo()
locals()[" fo o"]()比eval(foo())更可预测。
-
Michael Hoffman
Hi all:
Is there a metalanguage capability in Python (I know there are many) to
call a function having its name in a string?
Something like:
__call__("foo")
instead of:
foo()
Regards,
Tito
解决方案
On 7/27/05, Tito <ti*******************@gmail.com> wrote:Hi all:
Is there a metalanguage capability in Python (I know there are many) to
call a function having its name in a string?
Something like:
__call__("foo")
instead of:
foo()
def foo(): print "foobarred" .... foo() foobarred eval("foo()") foobarred
Peace
Bill Mill
bill.mill at gmail.com
Tito wrote:Hi all:
Is there a metalanguage capability in Python (I know there are many) to
call a function having its name in a string?
Something like:
__call__("foo")
instead of:
foo()
Regards,
Tito
eval(''foo()'') should do, but it''s said a bad practice ;)
Tito wrote:Hi all:
Is there a metalanguage capability in Python (I know there are many) to
call a function having its name in a string?
Something like:
__call__("foo")
instead of:
foo()
locals()["foo"]() will be a little more predictable than eval("foo()").
--
Michael Hoffman
这篇关于[Beginner]通过字符串中的名称调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文