python中通过string类名获得实例

原文:https://bytes.com/topic/python/answers/42866-how-create-object-instance-string

Ksenia Marasanova的回答。

该操作可实现类似java中的‘反射’

-------如果是在同一个命名空间
clazz = globals()['classname']
instance= clazz()

 ------如果在其它module,先import导入该module
import module
clazz= getattr(module, 'classname')
instance = clazz()

posted @ 2018-06-15 15:07  周睿  阅读(2688)  评论(0编辑  收藏  举报