【发布时间】:2013-07-31 08:46:27
【问题描述】:
我有课
class Foo():
def some_method():
pass
还有另一个类在同一个模块中:
class Bar():
def some_other_method():
class_name = "Foo"
# Can I access the class Foo above using the string "Foo"?
我希望能够使用字符串“Foo”访问Foo 类。
如果我在另一个模块中,我可以这样做:
from project import foo_module
foo_class = getattr(foo_module, "Foo")
我可以在同一个模块中做同样的事情吗?
IRC 中的人建议我使用映射字典,将字符串类名映射到类,但如果有更简单的方法,我不想这样做。
【问题讨论】:
-
是的,使用映射字典可能是正确的方法......
-
谢谢。我最终重构了,因为我不想使用
globals或使用映射字典。
标签: python