Skip to content

Instantly share code, notes, and snippets.

@mdellavo
Created February 2, 2017 14:16
Show Gist options
  • Save mdellavo/e032a9da68aeddef0e9f8fb33880cbe2 to your computer and use it in GitHub Desktop.
Save mdellavo/e032a9da68aeddef0e9f8fb33880cbe2 to your computer and use it in GitHub Desktop.
import inspect
import pkgutil
import importlib
def find_subsclasses(package, baseclass):
for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
if ispkg:
continue
module = importlib.import_module(package.__name__ + "." + modname, package.__name__)
for name in dir(module):
obj = getattr(module, name)
if inspect.isclass(obj) and issubclass(obj, baseclass):
yield obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment