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 6146349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:56:04+00:00 2026-05-23T18:56:04+00:00

I have python class trees, each made up of an abstract base class and

  • 0

I have python class trees, each made up of an abstract base class and many deriving concrete classes. I want all concrete classes to be accessible through a base-class method, and I do not want to specify anything during child-class creation.

This is what my imagined solution looks like:

class BaseClassA(object):
    # <some magic code around here>
    @classmethod
    def getConcreteClasses(cls):
        # <some magic related code here>

class ConcreteClassA1(BaseClassA):
    # no magic-related code here

class ConcreteClassA2(BaseClassA):
    # no magic-related code here

As much as possible, I’d prefer to write the “magic” once as a sort of design pattern. I want to be able to apply it to different class trees in different scenarios (i.e. add a similar tree with “BaseClassB” and its concrete classes).

Thanks Internet!

  • 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-23T18:56:05+00:00Added an answer on May 23, 2026 at 6:56 pm

    you can use meta classes for that:

    class AutoRegister(type):
        def __new__(mcs, name, bases, classdict):
            new_cls = type.__new__(mcs, name, bases, classdict)
            #print mcs, name, bases, classdict
            for b in bases:
                if hasattr(b, 'register_subclass'):
                    b.register_subclass(new_cls)
            return new_cls
    
    
    class AbstractClassA(object):
        __metaclass__ = AutoRegister
        _subclasses = []
    
        @classmethod
        def register_subclass(klass, cls):
            klass._subclasses.append(cls)
    
        @classmethod
        def get_concrete_classes(klass):
            return klass._subclasses
    
    
    class ConcreteClassA1(AbstractClassA):
        pass
    
    class ConcreteClassA2(AbstractClassA):
        pass
    
    class ConcreteClassA3(ConcreteClassA2):
        pass
    
    
    print AbstractClassA.get_concrete_classes()
    

    I’m personnaly very wary of this kind of magic. Don’t put too much of this in your code.

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

Sidebar

Related Questions

I have a python class with many methods(): Method1() Method2() ........... ........... MethodN() All
Suppose I have a Python class that I want to add an extra property
I have a python module that defines a number of classes: class A(object): def
Im having a problem with python.. I have a binary tree node type: class
I have a Python class full of static methods. What are the advantages and
I have a python class which reads a config file using ConfigParser: Config file:
If I have Python code class A(): pass class B(): pass class C(A, B):
If I have a python class, how can I alias that class-name into another
I have a Python file with as content: import re import urllib class A(object):
As part of the last assignment in a beginner python programing class, I have

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.