Custom Validating Descriptor
Create a descriptor class NonEmptyString that raises a ValueError if an assigned string is empty, and use it as the name attribute on a Product class.
Approach: implement _setname, get, and set__ on NonEmptyString, attach it as Product.name, then try constructing a Product with the given input and report success or the validation error.
Input: One line: a name (possibly empty).
Output: One line: "Product created: <name>" on success, or "Error: <message>" if the name is empty.
Shirt
Product created: Shirt
- input is a single line, possibly empty
Hint 1
__set_name__ runs once, when the descriptor is assigned to a class attribute — it's where self.name ("_name") gets set.
Hint 2
__set__ intercepts every assignment to product.name, including the one inside Product.__init__.
Hint 3
Raise ValueError(f"{self.name} cannot be empty") when value == "".
_setname_ fires once when NonEmptyString() is assigned as Product.name, recording the storage attribute name ("name"). Every subsequent assignment to product.name — including self.name = name inside init — goes through set, which raises ValueError for an empty string and otherwise stores the value via setattr. get then reads that stored value back out.