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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:19:41+00:00 2026-06-03T08:19:41+00:00

Usually Python descriptor are defined as class attributes. But in my case, I want

  • 0

Usually Python descriptor are defined as class attributes. But in my case, I want every object instance to have different set descriptors that depends on the input. For example:

class MyClass(object):
  def __init__(self, **kwargs):
    for attr, val in kwargs.items():
      self.__dict__[attr] = MyDescriptor(val)

Each object are have different set of attributes that are decided at instantiation time. Since these are one-off objects, it is not convenient to first subclass them.

tv = MyClass(type="tv", size="30")
smartphone = MyClass(type="phone", os="android")

tv.size   # do something smart with the descriptor

Assign Descriptor to the object does not seem to work. If I try to access the attribute, I got something like

<property at 0x4067cf0>

Do you know why is this not working? Is there any work around?

  • 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-03T08:19:43+00:00Added an answer on June 3, 2026 at 8:19 am

    This is not working because you have to assign the descriptor to the class of the object.

    class Descriptor:
    
        def __get__(...):
            # this is called when the value is got
    
        def __set__(...
        def __del__(...
    

    if you write

    obj.attr
    => type(obj).__getattribute__(obj, 'attr') is called
    => obj.__dict__['attr'] is returned if there else:
    => type(obj).__dict__['attr'] is looked up
    if this contains a descriptor object then this is used.
    

    so it does not work because the type dictionairy is looked up for descriptors and not the object dictionairy.

    there are possible work arounds:

    1. put the descriptor into the class and make it use e.g. obj.xxxattr to store the value.
      If there is only one descriptor behaviour this works.

    2. overwrite setattr and getattr and delattr to respond to discriptors.

    3. put a discriptor into the class that responds to descriptors stored in the object dictionairy.

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

Sidebar

Related Questions

I usually script/program using python but have recently begun programming with JavaScript and have
I usually write python in emacs. I'll often want to re-evaluate my file, which
In a Python system for which I develop, we usually have this module structure.
Two questions here. I have a set of files which are usually UTF-8 with
Python loves raising exceptions, which is usually great. But I'm facing some strings I
Languages like Ruby and Python are usually referred-to as open source, but what makes
Occasionally I want lazy module loading in Python. Usually because I want to keep
I have been writing command-line Python scripts for a while, but recently I felt
I have both Python 3.1 and Python 2.7 installed on my system. I'm usually
I usually perform things like this in C++, but I'm using python to write

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.