Loading exercise...

Exercise: Add a Property With Validation

Avatar image for AndersC

AndersC on May 23, 2026

In the .__init__() method, I had to write self.celsius = value for the test to pass. If I write self._celsius = value, I get an error message saying:

Raises ValueError for temperatures below absolute zero
Expected ValueError to be raised
Got: No exception raise

Why? In the lecture, it uses the non public attribute self._radius in the circle example.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on May 24, 2026

@AndersC Good question, and you’ve spotted a subtle but important distinction.

When you write self.celsius = value, Python looks up celsius on the class, finds the property, and runs the setter, which includes your validation. When you write self._celsius = value, you’re just setting a regular attribute directly, so the setter never runs and the validation is skipped. That’s why Temperature(-300) doesn’t raise ValueError in that case.

About the Circle example: notice that Chris assigns to self._radius inside the setter itself, not in .__init__(). He has to, because assigning to self.radius inside its own setter would call the setter again, causing infinite recursion. That’s the one place you must use the underscore name. Everywhere else (including .__init__()), you go through the property so the validation runs.

Avatar image for Miguel_T

Miguel_T on May 29, 2026

With self._radius = radius in the .__init__() as it is in the video, the validation is skipped when first creating an object for the reason Bartosz Zaczyński pointed out. You can test with Circle(-1). Shouldn’t the video match the solution of the exercise?

Become a Member to join the conversation.