Enforce an invariant through a data descriptor
Attribute lookup has an order. A data descriptor defines setting as well as getting, so it can enforce a rule before an instance dictionary value is considered. A read-only non-data descriptor has different precedence and can be shadowed.
Use a per-instance storage name established by __set_name__. Storing the value on the descriptor would share it among every instance of the owning class. The exercise checks independent owners, class access and validation of booleans, because bool is an int subclass and often slips through a domain-level integer check.
class Owner:
def __init__(self):
self._value=2
@property
def value(self):return self._value
@value.setter
def value(self,v):self._value=max(0,v)
x=Owner();x.value=-1
print(x.value)Review the design
Defining __set__ makes this a data descriptor. Its precedence protects ordinary assignment paths, but direct modification of backing storage remains possible; Python attribute conventions are not a security boundary.
Your task
Implement NonNegative descriptor. On assignment accept only type(value) is int and value>=0, otherwise raise ValueError. Unset instance values read as 0. Class access returns the descriptor.
The explanation opens here once every check passes.