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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:58:06+00:00 2026-06-14T19:58:06+00:00

I know there is no such a thing. That’s why I’m looking for some

  • 0

I know there is no such a thing. That’s why I’m looking for some nice equivalent. Having this class:

class MyClass:
   a = 5
   b  = "foo"
   c = False

I would like to “group” fields a and b together to be able to somehow iterate over members only from this group. So it would be nice to have some kind of field decorator, like:

class MyClass:
   @bar
   a = 5
   @bar
   b  = "foo"
   c = False

And to have some function like myDir(MyClass, ‘bar’) that would return (‘a’, ‘b’).

What options do we have so far?
1. Name these fields by special convention like ‘a_bar’, ‘b_bar’ – but I can’t do it unfortunately.
2. make a list of names as another class member – I would like to keep this grouping attribute close to attribute so I don’t like this approach.
3. Instead of assigning 5 to ‘a’ and “foo” to be I can make classes that inherit from integer and string and add another base class like ‘Group’ and then check the types – this way I will have to generate this class each time I have new type, so I don’t like this solution either.

Any other ideas?

  • 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-14T19:58:07+00:00Added an answer on June 14, 2026 at 7:58 pm

    Decorators are just syntactic sugar for a function call, so to apply a decorator to an attribute you just need to call it on the initialiser:

    a = bar(5)
    

    In your case you can create objects that implement the descriptor protocol:

    class GroupedAttribute(object):
        def __init__(self, group, obj):
            self.group = group
            self.obj = obj
        def __get__(self, obj, owner):
            return self.obj
        def __set__(self, obj, value):
            self.obj = value
    

    For elegance, you could write a class for attribute groups:

    class GroupedAttribute(object):
        def __init__(self, group, obj):
            self.group = group
            self.obj = obj
        def __get__(self, obj, owner):
            return self.obj
        def __set__(self, obj, value):
            self.obj = value
    
    class AttributeGroup(object):
        def __call__(self, obj):
            return GroupedAttribute(self, obj)
        def __get__(self, obj, owner):
            return BoundAttributeGroup(self, obj, owner)
    
    class BoundAttributeGroup(object):
        def __init__(self, group, obj, owner):
            self.group = group
            self.obj = obj
            self.owner = owner
        def __dir__(self):
            items = dir(self.owner if self.obj is None else self.obj)
            return [item for item in items if
                    getattr(self.owner.__dict__.get(item, None),
                            'group', None) is self.group]
    

    Usage:

    class MyClass(object):
        bar = AttributeGroup()
        a = bar(5)
        b = bar("foo")
        c = False
    dir(MyClass.bar)    # ['a', 'b']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there such a thing? I know that I can hook my function on
I know there is no such thing as a dumb question but this is:
I know there isn't such a thing as conditional compilation as in C/C++ but
In SQL Server 2008, I know there is no such thing as nested transactions.
In Google App Engine, there is such a thing as a ListProperty that allows
I know that there are a lot of posts about this issue, but I've
I know there is a StringIO stream in Python, but is there such a
I know there is such a question on SO, but I could not find
previously, there is such method for Rectangle in MFC, i dont know why there
I know there are a lot of such questions on stackoverflow but I couldn't

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.