<p>In Python, given a module X and a class Y, how can I iterate or generate a list of all subclasses of Y that exist in module X?</p>

[{'answer_id': 44381, 'author': 'Chris AtLee', 'author_id': 4558, 'author_profile': 'https://Stackoverflow.com/users/4558', 'pm_score': 5, 'selected': True, 'text': '<p>Here\'s one way to do it:</p>\n\n<pre><code>import inspect\n\ndef get_subclasses(mod, cls):\n    """Yield the classes in module ``mod`` that inherit from ``cls``"""\n    for name, obj in inspect.getmembers(mod):\n        if hasattr(obj, "__bases__") and cls in obj.__bases__:\n            yield obj\n</code></pre>\n'}, {'answer_id': 44403, 'author': 'Aaron Maenpaa', 'author_id': 2603, 'author_profile': 'https://Stackoverflow.com/users/2603', 'pm_score': 1, 'selected': False, 'text': "<p>Given the module foo.py</p>\n\n<pre><code>class foo(object): pass\nclass bar(foo): pass\nclass baz(foo): pass\n\nclass grar(Exception): pass\n\ndef find_subclasses(module, clazz):\n    for name in dir(module):\n        o = getattr(module, name)\n\n        try: \n             if issubclass(o, clazz):\n             yield name, o\n        except TypeError: pass\n\n&gt;&gt;&gt; import foo\n&gt;&gt;&gt; list(foo.find_subclasses(foo, foo.foo))\n[('bar', &lt;class 'foo.bar'&gt;), ('baz', &lt;class 'foo.baz'&gt;), ('foo', &lt;class 'foo.foo'&gt;)]\n&gt;&gt;&gt; list(foo.find_subclasses(foo, object))\n[('bar', &lt;class 'foo.bar'&gt;), ('baz', &lt;class 'foo.baz'&gt;), ('foo', &lt;class 'foo.foo'&gt;), ('grar', &lt;class 'foo.grar'&gt;)]\n&gt;&gt;&gt; list(foo.find_subclasses(foo, Exception))\n[('grar', &lt;class 'foo.grar'&gt;)]\n</code></pre>\n"}, {'answer_id': 47032, 'author': 'quamrana', 'author_id': 4834, 'author_profile': 'https://Stackoverflow.com/users/4834', 'pm_score': 2, 'selected': False, 'text': '<p>Can I suggest that neither of the answers from Chris AtLee and zacherates fulfill the requirements?\nI think this modification to zacerates answer is better:</p>\n\n<pre><code>def find_subclasses(module, clazz):\n    for name in dir(module):\n        o = getattr(module, name)\n        try:\n            if (o != clazz) and issubclass(o, clazz):\n                yield name, o\n        except TypeError: pass\n</code></pre>\n\n<p>The reason I disagree with the given answers is that the first does not produce classes that are a distant subclass of the given class, and the second includes the given class.</p>\n'}, {'answer_id': 408465, 'author': 'runeh', 'author_id': 2906, 'author_profile': 'https://Stackoverflow.com/users/2906', 'pm_score': 4, 'selected': False, 'text': "<p>Although Quamrana's suggestion works fine, there are a couple of possible improvements I'd like to suggest to make it more pythonic. They rely on using the inspect module from the standard library.</p>\n\n<ol>\n<li>You can avoid the getattr call by using <code>inspect.getmembers()</code></li>\n<li>The try/catch can be avoided by using <code>inspect.isclass()</code></li>\n</ol>\n\n<p>With those, you can reduce the whole thing to a single list comprehension if you like:</p>\n\n<pre><code>def find_subclasses(module, clazz):\n    return [\n        cls\n            for name, cls in inspect.getmembers(module)\n                if inspect.isclass(cls) and issubclass(cls, clazz)\n    ]\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/44352', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']