How can I get a list of all classes within current module in Python? How can I get a list of all classes within current module in Python? python python

How can I get a list of all classes within current module in Python?


Try this:

import syscurrent_module = sys.modules[__name__]

In your context:

import sys, inspectdef print_classes():    for name, obj in inspect.getmembers(sys.modules[__name__]):        if inspect.isclass(obj):            print(obj)

And even better:

clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass)

Because inspect.getmembers() takes a predicate.


What about

g = globals().copy()for name, obj in g.iteritems():

?


I don't know if there's a 'proper' way to do it, but your snippet is on the right track: just add import foo to foo.py, do inspect.getmembers(foo), and it should work fine.


matomo