Hello. I’m implementing a
Settings class. This class represents a collection of individual
Setting values. The code below is adapted from Raymond Hettinger’s Descriptor HowTo Guide.
class Setting(ABC): def __set_name__(self, owner, name): ... def __get__(self, instance, owner=None): def __set__(self, instance, value: T): ... def __delete__(self, instance): ... @abstractmethod def validate(self, value: T): pass
This class is inherited by
This solution is very elegant. Every derived class provides its own validator. The values are validated upon every assignment. The settings can print their names (using the well-known
if instance is None trick in
However, all these perks are lost when a sequence-like setting is needed. The
ListSetting validator is able to validate that it is assigned a
list, but it cannot validate the elements added to the list. Assigning elements to the list is completely out of control.
Now, I believe this problem has a few solutions, but is there a recommended (and equally elegant) one? For example, can I define descriptors on classes derived from mutable classes like