Skip to content

Instantly share code, notes, and snippets.

@harlowja
Created July 2, 2014 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harlowja/975533763a276bfe7fb2 to your computer and use it in GitHub Desktop.
Save harlowja/975533763a276bfe7fb2 to your computer and use it in GitHub Desktop.
import a
class B(a.A):
pass
def itersubclasses(cls, _seen=None):
"""Generator over all subclasses of a given class in depth first order."""
_seen = _seen or set()
subs = cls.__subclasses__()
for sub in subs:
if sub not in _seen:
_seen.add(sub)
yield sub
for sub in itersubclasses(sub, _seen):
yield sub
print list(itersubclasses(a.A))
import other_thing
print list(itersubclasses(a.A))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment