I'm writing some code in python 3.9 and have just recently discovered about descriptors.
I have created a class which looks like this:
import threading
class OuterClass():
class DecoratorTest():
def __init__(self,fget,fset=None) -> None:
self.fget=fget
self.fset=fset
self.event=threading.Event()
def __getattribute__(self, name: str):
return object.__getattribute__(self,name)
def __get__(self,obj,objtype=None):
return self.fget(obj)
def __set__(self,obj,value):
self.fset(obj,value)
self.event.set()
self.event.clear()
def setter(self,fset):
return type(self)(self.fget,fset)
def __init__(self):
self.random_value=10
@DecoratorTest
def test_property(self):
return self.random_value*2
@test_property.setter
def test_property(self,value):
self.random_value=value
My intent is to have a decorator which adds an event to the decorated property, so that when the property is changed, the event is set. This is an example of how I would use it:
class_iteration=OuterClass()
def test_funct():
while True:
class_iteration.test_property.event.wait()
print("Changed!")
threading.Thread(target=test_funct).start()
class_iteration.test_property=100
Problem is that when I try to get event, it considers class_iteration.test_property to be an int object, thus it throws an error. I wonder if there is any way to bypass the __get__ when using __getattribute__, so that I can access the event variable, or if I need to change my approach completely.