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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:52:36+00:00 2026-06-10T01:52:36+00:00

So I have a .py file containing a class where its subclasses can be

  • 0

So I have a .py file containing a class where its subclasses can be accessed as properties. All these subclasses are defined beforehand. I also need all the subclasses to have the same ability (having their own subclasses be accessible as properties). The biggest problem I’ve been facing is that I don’t know how to access the current class within my implementation of __getattr__(), so that’d be a good place to start.

Here’s some Python+Pseudocode with what I’ve tried so far. I’m pretty sure it won’t work since __getattr__() seems to be only working with instances of a class. If that is case, sorry, I am not as familiar with OOP in Python as I would like.

class A(object):
    def __getattr__(self, name):
        subclasses = [c.__name__ for c in current_class.__subclasses__()]
        if name in subclasses:
            return name
    raise AttributeError
  • 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-06-10T01:52:38+00:00Added an answer on June 10, 2026 at 1:52 am

    If I’ve understood your question properly, you can do what you want by using a custom metaclass that adds a classmethod to its instances. Here’s an example:

    class SubclassAttributes(type):
        def __getattr__(cls, name):  # classmethod of instances
            for subclass in cls.__subclasses__():
                if subclass.__name__ == name:
                    return subclass
            else:
                raise TypeError('Class {!r} has no subclass '
                                'named {!r}'.format(cls.__name__, name))
    
    class Base(object):
        __metaclass__ = SubclassAttributes  # Python 2 metaclass syntax
    
    #class Base(object, metaclass=SubclassAttributes):  # Python 3 metaclass syntax
    #    """ nothing to see here """
    
    class Derived1(Base): pass
    class Derived2(Base): pass
    
    print(Base.Derived1)  # -> <class '__main__.Derived1'>
    print(Base.Derived2)  # -> <class '__main__.Derived2'>
    print(Base.Derived3)  # -> TypeError: Class 'Base' has no subclass named 'Derived3'
    

    For something that works in both Python 2 and 3, define the class as shown below. Derives Base from a class that has SubclassAttributes as its metaclass. The is similar to what the six module’s with_metaclass() function does:

    class Base(type.__new__(type('TemporaryMeta', (SubclassAttributes,), {}),
                            'TemporaryClass', (), {})): pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file containing records delimited by the pattern /#matchee/. These records are
Say I have a variable containing PHP code, can I include its content as
I have a file containing some lines of code followed by a string pattern.
I have a file containing some data (for example, 00927E2B112DB958......). This data is a
I have a file containing lots of data put in a form similar to
I have a file containing a list of filenames: esocket.c esocket.h dockwin.cpp dockwin.h makefile
I have a file containing the following content 1000 line in the following format:
I have a file containing data in a single column .. I have to
I have a file containing, roughly speaking, the state of the application. I want
I have a file containing the information 0001:Jack:Jeremy:6:38.0 0002:Mat:Steve:1:44.5 0003:Jessy:Rans:10:50.0 0004:Van Red:Jimmy:3:25.75 0005:Peter:John:8:42.25 0006:Mat:Jeff:3:62.0

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.