The list of classes defined in the * .py file can be obtained using the built-in module “inspect”.
For example, for the “test_cls” module:
1 2 3 4 5 6 7 |
## test_cls.py class Test1: pass class Test2: pass |
we can get a list of classes with the following code:
1 2 3 4 5 6 7 8 |
## test.py import inspect classes = [cls_name for cls_name, cls_obj in inspect.getmembers(sys.modules['test_cls']) if inspect.isclass(cls_obj)] print(classes) # ['Test1', 'Test2'] |