This question already has an answer here:
这个问题已经有了答案:
- Calling a function of a module by using its name (a string) 10 answers
- 使用模块的名称(字符串)调用模块的函数10个答案
I have a str object for example: menu = 'install'
. I want to run install method from this string. For example when I call menu(some, arguments)
it will call install(some, arguments)
. Is there any way to do that ?
我有一个str对象,例如:menu = 'install'。我想从这个字符串运行install方法。例如,当我调用menu(some, arguments)时,它会调用install(some, arguments)。有什么办法吗?
3 个解决方案
#1
96
If it's in a class, you can use getattr:
如果是在类中,可以使用getattr:
class MyClass(object):
def install(self):
print "In install"
method_name = 'install' # set by the command line options
my_cls = MyClass()
method = None
try:
method = getattr(my_cls, method_name)
except AttributeError:
raise NotImplementedError("Class `{}` does not implement `{}`".format(my_cls.__class__.__name__, method_name))
method()
or if it's a function:
或者如果它是一个函数:
def install():
print "In install"
method_name = 'install' # set by the command line options
possibles = globals().copy()
possibles.update(locals())
method = possibles.get(method_name)
if not method:
raise NotImplementedError("Method %s not implemented" % method_name)
method()
#2
50
You can use a dictionary too.
你也可以用字典。
def install():
print "In install"
methods = {'install': install}
method_name = 'install' # set by the command line options
if method_name in methods:
methods[method_name]() # + argument list of course
else:
raise Exception("Method %s not implemented" % method_name)
#3
29
Why cant we just use eval()?
为什么我们不能使用eval()呢?
def install():
print "In install"
New method
新方法
def installWithOptions(var1, var2):
print "In install with options " + var1 + " " + var2
And then you call the method as below
然后调用方法如下所示
method_name1 = 'install()'
method_name2 = 'installWithOptions("a","b")'
eval(method_name1)
eval(method_name2)
This gives the output as
输出为
In install
In install with options a b
#1
96
If it's in a class, you can use getattr:
如果是在类中,可以使用getattr:
class MyClass(object):
def install(self):
print "In install"
method_name = 'install' # set by the command line options
my_cls = MyClass()
method = None
try:
method = getattr(my_cls, method_name)
except AttributeError:
raise NotImplementedError("Class `{}` does not implement `{}`".format(my_cls.__class__.__name__, method_name))
method()
or if it's a function:
或者如果它是一个函数:
def install():
print "In install"
method_name = 'install' # set by the command line options
possibles = globals().copy()
possibles.update(locals())
method = possibles.get(method_name)
if not method:
raise NotImplementedError("Method %s not implemented" % method_name)
method()
#2
50
You can use a dictionary too.
你也可以用字典。
def install():
print "In install"
methods = {'install': install}
method_name = 'install' # set by the command line options
if method_name in methods:
methods[method_name]() # + argument list of course
else:
raise Exception("Method %s not implemented" % method_name)
#3
29
Why cant we just use eval()?
为什么我们不能使用eval()呢?
def install():
print "In install"
New method
新方法
def installWithOptions(var1, var2):
print "In install with options " + var1 + " " + var2
And then you call the method as below
然后调用方法如下所示
method_name1 = 'install()'
method_name2 = 'installWithOptions("a","b")'
eval(method_name1)
eval(method_name2)
This gives the output as
输出为
In install
In install with options a b