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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:31:32+00:00 2026-06-12T17:31:32+00:00

Suppose I have a base class as following: class User: user_types = [] def

  • 0

Suppose I have a base class as following:

class User:
    user_types = []
    def __init__(self, name, age):
        self.name = name
        self.age = age

And then I want to create a subclass, which will then be added to user_types

class Farmer(User):
    def harvest(self):
        print("%s just harvested his plantation!" %self.name)

Now I can achieve what I’m trying to do, by simply saying

User.user_types.append(Farmer)

But how could I achieve this if I wanted it to be automatic, and to be done for every subclass? Let’s say there will be 400 subclasses, it would be a pain in the ass to manually do that for all of them.

  • 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-12T17:31:33+00:00Added an answer on June 12, 2026 at 5:31 pm

    New style classes have this functionality:

    >>> User.__subclasses__()
    [<class '__main__.Farmer'>]
    

    Just need to make User inherit from object in case you’re using Python 2.x.

    If you need all the subclasses of each subclass, you can still avoid metaclasses with this:

    def get_subclasses(c):
        subs = set(c.__subclasses__())
        return subs.union(*(get_subclasses(i) for i in subs))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have the following class hierarchy: class ClassA: @property def foo(self): return hello
Suppose that I have the following python base class: class BaseClass(object): def a(): This
Suppose I have the following (trivially simple) base class: public class Simple { public
Suppose I have the following: #include <iostream> #include <string> template<class T> class base {
Suppose we have the following class hierarchy: class Base { ... }; class Derived1
Suppose I have the following base class, Queen and Knight as its derivatives. WeaponBehaviour
For example, suppose I have the following base class for which I cannot modify
Suppose you have something like the following: class Shape // base class { private:
Suppose I have a Base class and its derived class Derived as follows: class
Suppose I have a Base class and a Child class that inherits from Base

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.