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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:46:16+00:00 2026-05-30T13:46:16+00:00

All, As the title asks, is it possible to change the __get__ method of

  • 0

All,
As the title asks, is it possible to change the __get__ method of a Descriptor at run time. I’m in a situation where I’ve got a function that is being decorated and undecorated on the the fly at run time. I’d like the result of this function to be available as a attribute, similar to what the @property does. I researched that and found it’s a descriptor, but it seems descriptors’ __get__ method is read only.

class Test( object ):
    def __init__( self ):
        self._x = 10

    def get_x( self ):
        return self._x

    @property
    def x( self ):
        return self.get_x()

The above code does what I want, roughly, in that

  1. The value is set in the constructor
  2. I can decorate the get_x method to my heart’s content
  3. instance.x returns the correct value

My issue is that I’d rather not have to create the get_x method since it’s basically unnecessary. I just haven’t been able to decorate the __get__ method of x as it is read-only.

Background

I’m writing a turn based strategy game, and I’m using decorators to implement persistent conditions. I’m able to implement these decorators effectively when I use test cases, but the issue is that to get the computed value then, you must use a function call, not an attribute access. This seems like an bad idea because getting values describing a unit would inconsistently use functions or attributes. I’d like to standardize on attributes if I can.

  • 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-30T13:46:18+00:00Added an answer on May 30, 2026 at 1:46 pm

    You can override default “read-only” characteristic of property‘s __get__ attribute using simple inheritance :

    class MyProperty( property ): pass
    
    class Test( object ):
        def __init__( self ):
            self._x = 10
    
        def get_x( self ):
            return self._x
    
        @MyProperty
        def x( self ):
            return self.get_x()
    
    test = Test()
    

    The problem now that even if you redefine __get__ attribure of your Text.x property, on test.x request python runtime will call MyProperty.__get__(Test.x, test, Test)

    So you could rewrite it only there like

    MyProperty.__get__ = lambda self,instance,owner: ""the x attribute"
    

    So good option here is to delegate call to some redifineable attribute like

    MyProperty.__get__ = lambda self,instance,owner: self.get(instance,owner)
    

    From now on get attribute of your property in your full control.
    Also there is bad option to generate separate type for each property-like object.
    So in good case you could do something like:

    class MyProperty( property ):
        def __get__(self,instance,owner) :
            if not instance: return self
            else: return self.get(instance,owner)
    
    class Test( object ):
        def __init__( self ):
            self._x = 10
    
        def get_x( self ):
            return self._x
    
        @MyProperty
        def x( self ): pass
    
        @MyProperty
        def y(self): pass
    
        x.get = lambda self,clazz: self.get_x()
        y.get = lambda self,clazz: "the y property of " + clazz.__name__ + " object"
    
    >>> test = Test()
    >>> test.x
    10
    >>> test.y
    'the y property of Test object'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title asks it all. The content on the site I'm building wont change
The title asks it all. I have found an example that looked something like
Title said it all. Some context: I got a search mechanism - search view,
As the title asks, what's the difference between Stop Debugging and Terminate All in
I know that h1 tag is important for SEO, so all my title is
Title asks it all, actually, but still, for completeness sake: Hi, I'm writing a
Actually the title contains all information needed. Is there an easy function like tableview.resizeColumnsToContents()
Title asks it all... I want to do a multi field - phrase search
Title kinda asks it all. I'm not referring to $_REQUEST , $_SERVER and all
I have a php web application that asks the user for all fields' values

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.