lib-not-dr/lib_not_dr/command/descriptor.py

14 lines
472 B
Python
Raw Normal View History

2023-07-11 14:02:05 +08:00
class CallBackDescriptor:
2023-07-11 14:24:33 +08:00
def __init__(self, name):
2023-07-11 14:02:05 +08:00
self.callback_name = name
def __set__(self, instance, value):
2023-07-11 14:24:33 +08:00
assert getattr(instance, self.callback_name) is None, f"Attribute '{self.callback_name}' has been set."
instance.__dict__[self.callback_name] = value
2023-07-11 14:02:05 +08:00
2023-07-11 14:24:33 +08:00
def __get__(self, instance, owner):
2023-07-11 15:33:01 +08:00
return (
self
if instance is None
else instance.__dict__.get(self.callback_name)
)