python的反射:动态获得模块,类,python模块,python的反射:动态


python的反射:动态获得模块,类:

__import__('theModuleFullName') #动态地导入模块m=sys.modules['theModuleFullName']#得到这个模块attstr=dir(m)#得到属性的列表for str in attstr:#迭代之    att=getattr(m,str)    #如果是类,而且是Father的子类    if type(att)==type and issubclass(att,Father):            theObj=att()#实例化 theObj.doSomething()#开始调用

评论关闭