Exercise 6: Class with Properties
Design a class with properties in this exercise.
We'll cover the following
Problem statement
Design a class, Circle
, that represents a geometric circle. The Circle
class initializer takes the radius of the circle as a positional parameter called radius
, which must be non-negative. Since the circle area will be used a lot, it should be stored as an area
attribute and must also be non-negative. Both attributes must be kept private and be accessible only through the namesake properties (radius
and area
). If any of the attributes are changed, the other must be recalculated accordingly. If a negative value is assigned to any attribute, a ValueError
exception must be raised.
Task
This exercise will require you to use private attributes and properties. For each property, create a simple getter and a more complex setter that validates the parameter and updates both the requested attribute and the other attribute. Remember that area=pi*r*r
and r=sqrt(area/pi)
.
Get hands-on with 1400+ tech skills courses.