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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:34:01+00:00 2026-05-24T01:34:01+00:00

Currently __setattr__ only works for instance. Is there any similar method for class? I

  • 0

Currently __setattr__ only works for instance. Is there any similar method for class? I am asking this question because I want to collect the list of defined attribute in order when user define it in class as below:

class CfgObj(object):
    _fields = []
    def __setattr__(self, name, value):
        self._fields.append([name, value])
        object.__setattr__(self, name, value)

class ACfg(CfgObj):
    setting1 = Field(str, default='set1', desc='setting1 ...')
    setting2 = Field(int, default=5, desc='setting2...')

I know the above code will not work as expected because the __setattr__ only called by instance as below:

acfg = ACfg()
acfg.c = 1
acfg._fields == [['c', 1]]

So, is there any equivalent __setattr__ for python class? The main purpose is to collect the define attribute in order when user define it in class.

  • 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-05-24T01:34:02+00:00Added an answer on May 24, 2026 at 1:34 am

    What I would do to solve your problem – but not your question – is to set the timestamp of the field creation create a counter of Field objects and set the current value of the counter to the created one:

    class Field(object):
        count = 0
        def __init__(self, value, default=None, desc=None):
            self.value = value
            self.default = default
            self.desc = desc
            # Here comes the magic
            self.nth = Field.count
            Field.count += 1
            # self.created_at = time.time()
    

    Then I would create a method for returning all fields ordered by its counter value:

    class CfgObj(object):
        def params(self):
            ns = dir(self)
            fs = [getattr(self, field) 
                        for field in ns 
                        if isinstance(getattr(self, field), Field)]
            # fs = sorted(fs, key=lambda f: f.created_at)
            fs = sorted(fs, key=lambda f: f.nth)
            return fs
    

    Its usage is intuitive:

    class ACfg(CfgObj):
        setting1 = Field(str, default='set1', desc='setting1 ...')
        setting2 = Field(int, default=5, desc='setting2...')
    
    print ACfg().params()
    

    Clearly the fields are ordered by time of object creation, not field creation, but it can be enough for you. Is it?

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

Sidebar

Related Questions

Currently, I don't really have a good method of debugging JavaScript in Internet Explorer and
Currently, if I want to output a SQL script for a table in my
Currently I know of only two ways to cache data (I use PHP but
Python noob here, Currently I'm working with SQLAlchemy, and I have this: from __init__
Currently am developing a program that solves (if possible) any given labyrinth of dimensions
Currently in my comp., there are some changes going on regarding project documentation. There
Currently I am debugging the signing of an Android app. And this would be
Currently, I have: <html> <div class=yes1><span><img src=img></span></div> </html> <script> var x = ='yes1' var
Currently I am adding object by creating it like: type TRecord = class private
I am a python learner and currently hacking up a class with variable number

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.