Я пишу дескриптор перманентного свойства, но когда я использую его он вовсе не работает как ему полагается!
Код:
class Permanent_property ():
def __init__(self, raise_exception: bool = True):
print(f'Init {self} with {raise_exception = }')
self.___exception = raise_exception
self.___instance = None
def __get__(self, obj, cls):
print(f'Get {self} for {obj} of {cls}')
return self.___instance
def __set__(self, obj, val):
print('Set {self} for {obj} {val = }')
if self.___instance is None:
self.___instance = val
else:
if self.___exception:
raise AttributeError('Permanent property for {obj} is alreadly setted')
Использование:
>>> class A ():
... def __init__ (self):
... self.attr = Permanent_property ()
>>> a = A()
Init <__main__.Permanent_property object at 0x0000023905744910> with raise_exception = True
>>> a.attr
<__main__.Permanent_property object at 0x0000023905744910>
И почему - то python не считает его как дескриптор свойств, а как обычное свойство. Что я делаю не так?