My goal is to fetch the list classes defined in a given Python file.
Following this link, I have implemented the following:
File b.py:
import imp
import inspect
module = imp.load_source("__inspected__", './a.py')
class_members = inspect.getmembers(module, inspect.isclass)
for cls in class_members:
class_name, class_obj = cls
member = cls[1]
print(class_name)
File a.py:
from c import CClass
class MyClass:
name = 'Edgar'
def foo(self, x):
print(x)
File c.py:
c_var = 2
class CClass:
name = 'Anna'
I have two issues with this implementation.
First, as is mentioned in the post, classes of imported module are printed out as well. I can't understand how to exclude them
Second, looks like the imp file is depreciated in favour of importlib, but the doc seems sketchy. And I can't figure out how to refactor my solution. Any hints ?