Using inspect.getmembers

I am trying to use inspect.getmembers to test classes and functions inside a file. The problem is that I do not know how to pass the file name to inspect.getmembers without using import . This is because I need to specify a different file name each time

The code looks something like this:

 def extractName(self,fileName): for name, obj in inspect.getmembers(FileName): if inspect.isclass(obj): print "this is class",name if inspect.isfunction(obj): print "this is method",name 
+4
source share
1 answer

To test a module, you must somehow execute it; otherwise, the definitions in the file will not be available.

You can use module = __import__(modname) to import a module by name or module = imp.load_source("__inspected__", path) to import a module by path.

+4
source

Source: https://habr.com/ru/post/1441030/


All Articles