0

I wanted not allow user to set minus number to radius, but I got maximum recursion depth exceeded in comparison. Im new and I don't understand it at all.

class Circle:
    def __init__(self, radius=1):
        if radius < 0:
            raise ValueError("Radius cannot be negative")
        else:
            self.radius = radius

@property
def radius(self):
    pass

@radius.setter
def radius(self, value):
    if value < 0:
        raise ValueError("Radius cannot be negative")
    else:
        self.radius = value

@property
def diameter(self):
    return self.radius * 2

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

@property
def area(self):
    return self.radius ** 2 * math.pi

@area.setter
def area(self, value):
    self.radius = math.sqrt(value / math.pi)
WiX
  • 3
  • 2
  • 1
    `self.radius = value` will keep calling the setter over and over. You need another variable for radius. [What's the pythonic way to use getters and setters?](https://stackoverflow.com/a/2627034) – 001 Jul 16 '22 at 13:10
  • Does this answer your question? [Python - Property setting from list causes maximum recursion depth exceeded](https://stackoverflow.com/questions/18989362/python-property-setting-from-list-causes-maximum-recursion-depth-exceeded) – DarrylG Jul 16 '22 at 13:11
  • See: [Using Properties in Python classes cause "maximum recursion depth exceeded](https://stackoverflow.com/questions/36931415/using-properties-in-python-classes-cause-maximum-recursion-depth-exceeded) – DarrylG Jul 16 '22 at 13:14

0 Answers0