Answer the question
In order to leave comments, you need to log in
Why doesn't the handle work as it should?
I'm writing a permanent property handle, but when I use it it doesn't work at all like it's supposed to!
The code:
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>
Answer the question
In order to leave comments, you need to log in
You open the dock.
Reading:
Descriptors only work when used as class variables. When put in instances, they have no effect.
class A:
attr = Permanent_property()
def __init__(self, something):
self.attr = something
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question