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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:35:14+00:00 2026-06-08T23:35:14+00:00

I can see multiple roadblocks to making this work, and it may well be

  • 0

I can see multiple roadblocks to making this work, and it may well be impossible, but I thought I’d at least ask…

So, I have a class AbstractEnumeratedConstantsGroup that, unsurprisingly, when subclassed creates an object that represents a group of enumerated constants (ex FLAVORS with values ('VANILLA', 'CHOCOLATE', 'STRAWBERRY') where FLAVORS[0] returns 'VANILLA', FLAVORS[1] returns 'CHOCOLATE', FLAVORS.VANILLA returns 0, FLAVORS.CHOCOLATE returns 1, etc) it uses a metaclass and some other footwork so that all that is required to create a new constants group is:

class FLAVORS(AbstractEnumeratedConstantsGroup):
    _constants = ('VANILLA', 'CHOCOLATE', 'STRAWBERRY')
FLAVORS = FLAVORS()

I’d like to reduce it further to:

@enumerated_constants_group
FLAVORS = ('VANILLA', 'CHOCOLATE', 'STRAWBERRY')

The problems I don’t know how to get past are:

    1. No good way to get the label name for the object from the defining scope from the decorator code
    2. Can’t decorate something that isn’t a class, method, or function

I’ve considered using a factory/builder, but what I don’t like about that is that it requires duplication of the group name in the code, or that the groups have unhelpful __name__ values (which are used in __repr__ and __str__). Ex:

FLAVOR = enumerated_constants_group('FLAVOR', ('VANILLA', 'CHOCOLATE', 'STRAWBERRY'))

or

FLAVOR = enumerated_constants_group(('VANILLA', 'CHOCOLATE', 'STRAWBERRY'))
# FLAVOR.__name__ becomes some unfriendly machine-generated string

As an alternative to the decorator idea, would it be possible to reference the calling scope (through introspection or from implicitly passing it to the function) from a factory method such that calling the method would insert the newly created object in to the calling namespace with the given name? Ex:

enumerated_constants_group('FLAVOR', ('VANILLA', 'CHOCOLATE', 'STRAWBERRY'))
# I could now reference FLAVOR in any arbitrary scope that the method was called from

Is there anything I can do to achieve what I want? Are there other approaches I haven’t thought of?

  • 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-08T23:35:16+00:00Added an answer on June 8, 2026 at 11:35 pm

    There is no general way to affect what happens when you assign to a bare name (as in FLAVORS = ('VANILLA', 'CHOCOLATE', 'STRAWBERRY')). So what you want is not possible.

    That said, you could write a metaclass that replaces the returned class with an instance of itself, so that you don’t need your last FLAVORS = FLAVORS(...) line in your first example. The class definition itself would suffice to create the object.

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

Sidebar

Related Questions

I can see some potential difficulties with this concept but the idea is: I
I have the following query which as you can see does multiple Count(CompetitorID) calls.
As you can see from the screenshot below I have a ListView with multiple
I have multiple documents open in Geany. I can see them listed in the
I'm using this http://jqueryui.com/demos/autocomplete/#multiple-remote on my website, I can see this code 'searches' search.php
Is it possible to overlay multiple JSliders so I can see the thumbs on
I can see that GD library is for images. But I can't see differences
I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but
I have this command (visibleSelect is jquery variable that holds multiple select list): var
I can see columns that list people names in SharePoint's datasheet view, but read-only

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.