このようなクラスがあるとしましょう
class MyClass(AnotherClass):
def __init__(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2
def f(self):
#dostuff
そして、「私のクラス」という文字列があります
str = "my class"
私がすることができます:
class_name_from_str = str.title().replace(" ", "") # now class_name_from_str is "MyClass"
class_name_from_str を呼び出し可能にするにはどうすればよいですか? 私はこのようなものが欲しい:
callable_obj = some_process(class_name_from_str)
o = callable_obj(arg1, arg2)
o.f()
ちなみに、私はPython 2.7を使用しています
更新: Matti Virkkunenがコメントで示唆したように、良い解決策は、文字列をクラスにマップする辞書を作成することです
my_calls_dict = {'my class' : MyClass, 'my other class' : MyOtherClass}