Problem Statement
When would you choose a custom descriptor over @property?
Explanation
Use property for one attribute on one class. It is simple and local. Choose a custom descriptor when the same validation or behavior must be reused across many classes or many attributes.
Descriptors centralize logic and remove repetition, such as non-empty strings, ranged numbers, or type-checked fields. They also work with class attributes and can coordinate with metaclasses if you need schema-like behavior.
Code Solution
SolutionRead Only
class NonEmpty:
def __set__(self,obj,val):
if not val: raise ValueError('empty')
obj.__dict__['name']=val
class User:
name = NonEmpty()
def __init__(self, name): self.name=name