How to instantiate a class given its name as a string?

This question already has an answer here: Calling a function of a module by using its name (a string) 10 answers Considering that the Python naming scheme is based on maps, you will be using maps of one sort or another even if you use eval . Building your own map is the most appropriate, but you can also use globals() or retrieve it from a module if your classes are stored somewhere else:

如何实例化一个给定其名称为字符串的类?

这个问题在这里已经有了答案: 通过使用其名称(字符串)调用模块的功能10个答案 考虑到Python命名方案基于地图,即使使用eval ,您也将使用这种或那种地图。 构建你自己的映射是最合适的,但是如果你的类存储在别的地方,你也可以使用globals()或者从模块中检索它: import util_module getattr(util_module, 'A') ()

Calling unspecified function from another file and another class

This question already has an answer here: Calling a function of a module by using its name (a string) 10 answers class Options(object): def functionOne(self): # or function1 print 'one' def functionTwo(self): # or function2 print 'two' def functionThree(self): # or function3 print 'three' o = Options() for name in dir(o): if 'function' in name:

从另一个文件和另一个类调用未指定的函数

这个问题在这里已经有了答案: 通过使用其名称(字符串)调用模块的功能10个答案 class Options(object): def functionOne(self): # or function1 print 'one' def functionTwo(self): # or function2 print 'two' def functionThree(self): # or function3 print 'three' o = Options() for name in dir(o): if 'function' in name: getattr(o, name)() 执行时,上述结果

How to assign class method to class attribute?

This question already has an answer here: Calling a function of a module by using its name (a string) 10 answers Note that when you try: class A: MAP = { 'add' : A.add(x, y), 'subtract' : A.subtract(x, y), } you are trying to access eg A.add before the name A exists (the class isn't bound to the name until definition completes) and before the name add exists (you

如何将类方法分配给类属性?

这个问题在这里已经有了答案: 通过使用其名称(字符串)调用模块的功能10个答案 请注意,当您尝试: class A: MAP = { 'add' : A.add(x, y), 'subtract' : A.subtract(x, y), } 在名称A存在之前(该类未与名称绑定直到定义完成)以及在名称add存在之前(您尚未定义该方法),您正尝试访问例如A.add 。 类定义顶层的所有内容都按顺序完成。 在类定义之后 ,您需要将类方法放入字典中(直到定义

Call a function using getattr in Python

This question already has an answer here: Calling a function of a module by using its name (a string) 10 answers It's a bit hard to see what exactly you're trying to accomplish without seeing what you've tried so far. Nonetheless, if the function you want to call is actually a method on some object, you can use getattr: # somestring is a string, but could be any object somestri

在Python中使用getattr调用一个函数

这个问题在这里已经有了答案: 通过使用其名称(字符串)调用模块的功能10个答案 没有看到你到目前为止尝试完成的事情,你很难看到你想要完成什么。 但是,如果要调用的函数实际上是某个对象上的方法,则可以使用getattr: # somestring is a string, but could be any object somestring = "valentyn" # somestring.upper happens to be a method method = getattr(somestring, "upper") # ...which can be called in t

Making a menu in Python

Possible Duplicate: Calling a function from a string with the function's name in Python I think I could write some terrible code that would do this, but I'd much rather see the 'clean version'. What seems the good approach to me, is to make a dict that holds the various functions that a given object can use. Then when the user is instructed to tell the object what it's

在Python中制作菜单

可能重复: 在Python中使用函数名称调用函数 我想我可以写一些可怕的代码来做到这一点,但我更愿意看到'干净的版本'。 对我而言,最好的方法是制作一个字典,其中包含给定对象可以使用的各种功能。 然后,当用户被指示告诉对象它正在做什么时,它会根据该字典吐出一个菜单。 我搜索了一下,并没有看到适用于我的东西,所以我想我会试试看。 那么,它没有工作。 class Man(object): def __init__(self):

Call a function from a stored string in Python

This question already has an answer here: Calling a function of a module by using its name (a string) 10 answers 你可以这样做 : eval(input("What function do you want to call? ") + '()') 使用一个字典映射名称的功能。 call_dict = { 'foo': foo, 'bar': bar } call_dict[callfunction]() It is quite common in Python to use the command pattern. First move all of your functions into a class, a

从Python中存储的字符串调用函数

这个问题在这里已经有了答案: 通过使用其名称(字符串)调用模块的功能10个答案 你可以这样做 : eval(input("What function do you want to call? ") + '()') 使用一个字典映射名称的功能。 call_dict = { 'foo': foo, 'bar': bar } call_dict[callfunction]() 在Python中使用命令模式很常见。 首先将所有函数移动到一个类中,并为它们指定名称,该名称的前缀未在输入中使用。 然后使用getattr()来查找正确的函

Python Call Function from String

This question already has an answer here: Calling a function of a module by using its name (a string) 10 answers you could use exec. Not recommended but doable. s = "func()" exec s def func(): print("hello") s = "func" eval(s)() In [7]: s = "func" In [8]: eval(s)() hello Not recommended! Just showing you how. The safest way to do this: In [492]: def fun(): .....: print("

来自字符串的Python调用函数

这个问题在这里已经有了答案: 通过使用其名称(字符串)调用模块的功能10个答案 你可以使用exec。 不推荐,但可行。 s = "func()" exec s def func(): print("hello") s = "func" eval(s)() In [7]: s = "func" In [8]: eval(s)() hello 不建议! 只是告诉你如何。 最安全的方法是: In [492]: def fun(): .....: print("Yep, I was called") .....: In [493]: locals()['fun']() Yep, I was call

How do I use user input to invoke a function in Python?

This question already has an answer here: Calling a function of a module by using its name (a string) 10 answers Create a raw input with commands inside a Python script 1 answer The error is because function names are not string you can't call function like 'func1'() it should be func1() , You can do like: { 'func1': func1, 'func2': func2, 'func3': func3, }.get(choice)()

我如何使用用户输入来调用Python中的函数?

这个问题在这里已经有了答案: 通过使用其名称(字符串)调用模块的功能10个答案 使用Python脚本中的命令创建一个原始输入1答案 错误是因为函数名称不是字符串,你不能调用像'func1'()函数它应该func1() , 你可以这样做: { 'func1': func1, 'func2': func2, 'func3': func3, }.get(choice)() 它通过将字符串映射到函数引用 方面说明 :你可以编写一个默认的函数,如: def notAfun(): print "not a

How to invoke an function on an object dynamically by name?

This question already has an answer here: Calling a function of a module by using its name (a string) 10 answers 使用“getattr”:http://docs.python.org/library/functions.html?highlight=getattr#getattr obj = MyClass() try: func = getattr(obj, "dostuff") func() except AttributeError: print "dostuff not found"

如何根据名称动态调用对象的函数?

这个问题在这里已经有了答案: 通过使用其名称(字符串)调用模块的功能10个答案 使用“getattr”:http://docs.python.org/library/functions.html?highlight=getattr#getattr obj = MyClass() try: func = getattr(obj, "dostuff") func() except AttributeError: print "dostuff not found"

Use a string to call function in Python

This question already has an answer here: Calling a function of a module by using its name (a string) 10 answers You could use eval(): myString = "fullName( name = 'Joe', family = 'Brand' )" result = eval(myString) Beware though, eval() is considered evil by many people. This does not exactly answer your question, but maybe it helps nevertheless: As mentioned, eval should be avoided if

使用一个字符串在Python中调用函数

这个问题在这里已经有了答案: 通过使用其名称(字符串)调用模块的功能10个答案 你可以使用eval(): myString = "fullName( name = 'Joe', family = 'Brand' )" result = eval(myString) 但要小心, eval()被许多人认为是邪恶的。 这不完全回答你的问题,但也许它有帮助: 如前所述,如果可能的话应该避免使用eval 。 更好的方法是使用字典解包。 这也是非常动态的,并且不易出错。 例: def fullName(name = "