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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:48:40+00:00 2026-06-12T00:48:40+00:00

I have a bunch of classes that get persisted in a ZODB. All classes

  • 0

I have a bunch of classes that get persisted in a ZODB. All classes have to share some indexing features, but also all of them define a lot of properties. The properties are defined on class level with descriptors, to be able to intercept writing to them for validation and check for duplicates in the DB. For this, every class has a list of the automatically maintained properties, _properties. They also hold _indices and some other features, which I don’t want to copy on my own to every single Element-class (about 10 or so).

Consider the following code:

class BaseElement(object):
    _properties = None

class ElementFlavour1(BaseElement):
    _properties = 'a', 'b', 'c'

....

class ElementFlavourN(BaseElement):
    _properties = 'e', 'f', 'g'

for item in locals():
    if issubclass(item, BaseElement):
        item._v_properties_set = set(item._properties) if item._properties else set()

So, it basically does what it should do: I need to iterate the _properties linearly, but I also want to do quick lookups, if some property is present in a class. Nevertheless, I don’t want to repeat myself and specify this set for every single flavour of BaseElement explicitly.

The current solution works, but I consider it as an ugly hack and would like to get rid of it. E.g. subclasses of BaseElement in other modules wouldn’t get their properties set right. Playing around with __new__ also seems to be the wrong way, as it doesn’t intercept the class construction, but the instantiation. So, how to do that properly?

P.S. I’m aware of OrderedDict, but this is not what I’m looking for. I’d like to hook into the class creation process in a clean way and append arbitrary functionality there.

  • 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-12T00:48:41+00:00Added an answer on June 12, 2026 at 12:48 am

    You could do this with a metaclass:

    class BaseType(type):
        def __init__(cls, name, bases, clsdict):
            super(BaseType, cls).__init__(name, bases, clsdict)
            setattr(cls,'_v_properties_set',
                    set(cls._properties) if cls._properties else set())
    
    class BaseElement(object):
        __metaclass__ = BaseType    
        _properties = None
    
    class ElementFlavour1(BaseElement):
        _properties = 'a', 'b', 'c'
    
    class ElementFlavourN(BaseElement):
        _properties = 'e', 'f', 'g'
    
    print(ElementFlavourN._v_properties_set)
    # set(['e', 'g', 'f'])
    

    You could also do this with a class decorator, but then you’d have to decorate each subclass of BaseElement individually. The metaclass is inherited, so you only have to define it once.

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

Sidebar

Related Questions

I have a bunch of classes that all contain a Shared ReadOnly Dictionary. If
Here is my problem. I have a bunch of Configuration classes that get serialized
Let's say I have a bunch of classes that look something like... class Foo{
I have a bunch of third-party Java classes that use different property names for
In my program, I've got a bunch of classes that will only ever have
I have a web application that contains a bunch of classes in the App_Code
imagine I have a bunch of C++ related classes (all extending the same base
I have a bunch of classes that I intend to serialize in order to
I have a bunch of classes in a CUDA project that are mostly glorified
I have a bunch of classes that i have to tweak for improved memory

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.