python 遍历模块下py文件中的类

import os
import inspect
import importlib

from xview.tests.foundation.base_resource import BaseResource

path = os.path.dirname(os.path.abspath(__file__))
resource_mod = 'xview.tests.resources'

all_resources = list()
for f in os.listdir(path):
if f != '__init__.py':
file_mode = '%s.%s' % (resource_mod, os.path.splitext(f)[0])
mode_obj = importlib.import_module(file_mode)
classes = inspect.getmembers(mode_obj, inspect.isclass)
for name, cls in classes:
if cls.__base__.__name__ == BaseResource.__name__:
all_resources.append(cls)


posted @ 2022-05-05 18:29  aaron_agu  阅读(370)  评论(0编辑  收藏  举报