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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:03:48+00:00 2026-06-05T21:03:48+00:00

What I would like to do there is declaring class variables, but actually use

  • 0

What I would like to do there is declaring class variables, but actually use them as vars of the instance. I have a class Field and a class Thing, like this:

class Field(object):
    def __set__(self, instance, value):
        for key, v in vars(instance.__class__).items():
            if v is self:
                instance.__dict__.update({key: value})

    def __get__(self, instance, owner):
        for key, v in vars(instance.__class__).items():
            if v is self:
                try:
                    return instance.__dict__[key]
                except:
                    return None


class Thing(object):
    foo = Field()

So when I instantiate a thing and set attribute foo, it will be added to the instance, not the class, the class variable is never actually re-set.

new = Thing()
new.foo = 'bar'
# (foo : 'bar') is stored in new.__dict__

This works so far, but the above code for Field is rather awkward. It has too look for the Field object instance in the classes props, otherwise there seems no way of knowing the name of the property (foo) in __set__ and __get__. Is there another, more straight forward way to accomplish this?

  • 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-05T21:03:50+00:00Added an answer on June 5, 2026 at 9:03 pm

    Every instance of Field (effectively) has a name. Its name is the attribute name (or key) which references it in Thing. Instead of having to look up the key dynamically, you could instantiate Fields with the name at the time the class attribute is set in Thing:

    class Field(object):
        def __init__(self, name):
            self.name = name
    
        def __set__(self, instance, value):
            instance.__dict__.update({self.name: value})
    
        def __get__(self, instance, owner):
            if instance is None:
                return self
            try:
                return instance.__dict__[self.name]
            except KeyError:
                return None
    
    def make_field(*args):
        def wrapper(cls):
            for arg in args:
                setattr(cls, arg, Field(arg))
            return cls
        return wrapper
    
    @make_field('foo')
    class Thing(object):
        pass
    

    And it can be used like this:

    new = Thing()
    

    Before new.foo is set, new.foo returns None:

    print(new.foo)
    # None
    

    After new.foo is set, 'foo' is an instance attribute of new:

    new.foo = 'bar'
    print(new.__dict__)
    # {'foo': 'bar'}
    

    You can access the descriptor (the Field instance itself) with Thing.foo:

    print(Thing.foo)
    # <__main__.Field object at 0xb76cedec>
    

    PS. I’m assuming you have a good reason why

    class Thing(object):
        foo = None
    

    does not suffice.

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

Sidebar

Related Questions

Is there a syntax for declaring anonymous arrays in BeanShell? I would like to
I would like to learn how setup prefix routing like there is in cakephp
I would like ask if there's a way to download an android layout from
I would like to know if there exists any kind of library or workaround
I would like to know if there is any kind of tool to monitor
I would like to know if there is a method to create share buttons
I would like to know if there's a way in java to find out
I would like to know is there any difference in performance between these two
I would like to know if there is a way to execute polygons queries
I would like to know if there are any good resources (online or books)

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.