Imagine you have a Circle class where you want the radius to always be non-negative. Which approach would you use to ensure this while still allowing direct assignment like circle.radius = 5?

  • Making the radius attribute private and providing a public method to set it.
  • Not allowing direct assignment to the radius attribute.
  • Using a property with a setter method that checks and enforces the non-negativity of the radius.
  • Using the @property decorator without a setter method.
You would use a property with a setter method to validate the radius and ensure it's non-negative while still allowing direct assignment. The setter method can perform the check.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *