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

  • SEARCH
  • Home
  • 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 7981177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:17:39+00:00 2026-06-04T10:17:39+00:00

class A(object): _all = set() def __new__(cls): obj = super(A,cls).__new__(cls) cls._all.add(obj) return obj class

  • 0
class A(object):
    _all = set()

    def __new__(cls):
        obj = super(A,cls).__new__(cls)
        cls._all.add(obj)
        return obj

class B(A):
    pass

class C(A):
    pass

b = B()
c = C()

This will create all instances and put them in A._all. What I want is for the instances of C to be placed in C._all (and similarly for C).

I could define B and C like this:

class B(A):
    _all = set()

class C(A):
    _all = set()

Which would work. However, this feels like repeating code, and if I forget to add _all when I subclass A next, or misspell _alll I’m likely to get hard to debug errors.

How would I go about doing this well?

  • 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-04T10:17:40+00:00Added an answer on June 4, 2026 at 10:17 am

    Here’s a metaclass for it:

    class make_all_meta(type):
       def __init__(cls, name, bases, dic):
           type.__init__(cls, name, bases, dic)
           cls._all = set()
    
    class A(object):
        __metaclass__ = make_all_meta
    

    In Python 3, the definition for A must be:

    class A(metaclass=make_all_meta):
        pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Ruby, can one object destroy another? For example: class Creature def initialize @energy
I have the following code. class person(object): def __init__(self, keys): for item in keys:
Here's the faulty bit of my code: class Room(object): def __init__(self): self.hotspots = []
I have a python class/object as follows. class Hello: def __init__(self): self.x = None
In Java, all classes are extending Object class implicitly , on top of that
I have a number of objects, all from the same class(ColorNum) Each object has
I know the class SimpleXMLEncoder can encode all properties of an Object in XML.
I was attempting to store all the JMS object of the class Message recieved
I wrote an abstraction class for a math object, and defined all of the
We have the Class object (an object that reference a Class) so you can

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.