Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1838608
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:30:04+00:00 2026-05-17T06:30:04+00:00

I need a working approach of getting all classes that are inherited from a

  • 0

I need a working approach of getting all classes that are inherited from a base class in Python.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:30:04+00:00Added an answer on May 17, 2026 at 6:30 am

    New-style classes (i.e. subclassed from object, which is the default in Python 3) have a __subclasses__ method which returns the subclasses:

    class Foo(object): pass
    class Bar(Foo): pass
    class Baz(Foo): pass
    class Bing(Bar): pass
    

    Here are the names of the subclasses:

    print([cls.__name__ for cls in Foo.__subclasses__()])
    # ['Bar', 'Baz']
    

    Here are the subclasses themselves:

    print(Foo.__subclasses__())
    # [<class '__main__.Bar'>, <class '__main__.Baz'>]
    

    Confirmation that the subclasses do indeed list Foo as their base:

    for cls in Foo.__subclasses__():
        print(cls.__base__)
    # <class '__main__.Foo'>
    # <class '__main__.Foo'>
    

    Note if you want subsubclasses, you’ll have to recurse:

    def all_subclasses(cls):
        return set(cls.__subclasses__()).union(
            [s for c in cls.__subclasses__() for s in all_subclasses(c)])
    
    print(all_subclasses(Foo))
    # {<class '__main__.Bar'>, <class '__main__.Baz'>, <class '__main__.Bing'>}
    

    Note that if the class definition of a subclass hasn’t been executed yet – for example, if the subclass’s module hasn’t been imported yet – then that subclass doesn’t exist yet, and __subclasses__ won’t find it.


    You mentioned “given its name”. Since Python classes are first-class objects, you don’t need to use a string with the class’s name in place of the class or anything like that. You can just use the class directly, and you probably should.

    If you do have a string representing the name of a class and you want to find that class’s subclasses, then there are two steps: find the class given its name, and then find the subclasses with __subclasses__ as above.

    How to find the class from the name depends on where you’re expecting to find it. If you’re expecting to find it in the same module as the code that’s trying to locate the class, then

    cls = globals()[name]
    

    would do the job, or in the unlikely case that you’re expecting to find it in locals,

    cls = locals()[name]
    

    If the class could be in any module, then your name string should contain the fully-qualified name – something like 'pkg.module.Foo' instead of just 'Foo'. Use importlib to load the class’s module, then retrieve the corresponding attribute:

    import importlib
    modname, _, clsname = name.rpartition('.')
    mod = importlib.import_module(modname)
    cls = getattr(mod, clsname)
    

    However you find the class, cls.__subclasses__() would then return a list of its subclasses.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need help getting Ember-Data working with Zend Rest. At first, I'm familiar with Zend
I need help getting CommonJS working on Java 7 and Rhino 1.7R3. Rhino 1.7R3
Suppose we have 2 classes, Child, and the class from which it inherits, Parent.
For a project I'm working I need to have some sort of enumaration class
I have had problems getting Bootstrap popovers working with dynamically created elements (but that's
I need help working with very big numbers. According to Windows calc, the exponent
I have a jQuery code, but need it working by using Mootools: if (
I need to begin working with milliseconds in .Net 3.0. The data will be
I working on Eclipse plugin and i need to perform drag&drop support to editor.
I need to understand the working of this particular program, It seems to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.