Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Advanced Objects Special Methods

Simon Amz
Simon Amz
4,606 Points

Radius Setter or diameter

Hi, in this video, I made some confusion about setter. Indeed, for the setting of radius attribute, you wrote:

    @radius.setter
    def radius(self, radius):
        self.diameter = radius * 2

However, in this setter, this is the diameter which is set. I don't understand why we talk about radius setter. Shouldn't we write this:

    @radius.setter
    def radius(self, radius):
        self.radius = radius

Thanks for your response

[MOD: added ```python formatting -cf]

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Great question! In the Circle class there is only one defined attribute diameter. Kenneth then adds methods to get the effective radius based on the diameter attribute. An attribute "radius" could have been define, but this can lead to a consistency issue: How to keep the values of diameter and radius always in agreement?

Instead of have both radius and diameter attributes and have both methods update both attributes, It is easier to maintain one attribute which both methods use.

Post back if you have more questions. Good luck!

Simon Amz
Simon Amz
4,606 Points

Thanks Chris,

So to summarise, only one attribute is defined (here diameter) and the radius setter, use the attribute diameter, to keep this relation :

diameter = radius * 2 . && . radius = diameter / 2

right?

If so, got it.

Best,