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)
文章出处:http://www.cnblogs.com/aaron-agu/
只有毅力和决心才能使人真正具有价值!