HardPro challengePython
Descriptor Protocol — Validated Attribute
PythonMetaprogrammingDescriptors
Implement a PositiveNumber descriptor that:
- Allows setting only positive numbers (> 0). Raises
ValueErrorfor zero or negatives. - Returns the stored value on get.
- Stores values per-instance (not on the descriptor itself).
class Circle:
radius = PositiveNumber()
c = Circle()
c.radius = 5 # OK
c.radius # → 5
c.radius = -1 # raises ValueErrorImplement solve(value) that creates a Circle, sets its radius = value and returns the radius. If value <= 0, return the string "error".
Sample tests
Test #1Positive value stored and returned
Input: [5]
Output: 5
Test #2Negative value raises ValueError
Input: [-1]
Output: "error"
Test #3Zero raises ValueError
Input: [0]
Output: "error"