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
- The value is set in the constructor
- I can decorate the
get_xmethod to my heart’s content instance.xreturns 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.
You can override default “read-only” characteristic of
property‘s__get__attribute using simple inheritance :The problem now that even if you redefine
__get__attribure of your Text.x property, ontest.xrequest python runtime will callMyProperty.__get__(Test.x, test, Test)So you could rewrite it only there like
So good option here is to delegate call to some redifineable attribute like
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: