The circle Class seems to be designed to prevent its attribute radius
from receiving negative values through the use of the property feature and a defensive setter __setRadius
Whilst this prevents a caller from supplying a non positive value once the object has been created, it does not stop this happening when the object is created:
will set the radius to the negative value because the setter is not invoked in the initialiser.
Curiously, if you try to do this by changing
self.__radius = radius
self.__radius = self.__setRadius(radius)
, the interpreter does not complain but the assignment does not work with a positive value, instead radius
is set to None
. It does, however, work if you repeat the check / raising of ValueError for a non positive value.
I am also a little surprised that the design prevents the caller from reading the value of the radius when the property mechanism allows them to alter it, as this seems somewhat bizarre behaviour. This can be overcome by introducing a getter and altering the property assignment along the following lines:
radius = property(__getRadius, __setRadius)