Question
I'm learning setters and Python 3 coding. I'm not very good at it and have been struggling with this problem for a while now. I
I'm learning setters and Python 3 coding. I'm not very good at it and have been struggling with this problem for a while now. I need some additional help as my code is not acting the way I want it to. What is the best way to execute the following code?
What I want:
>>>circle = Circle()
>>>circle
Circle(radius=1)
>>>circle.radius_log
[1]
What I am getting:
>>>circle = Circle()
>>>circle
Circle(radius=1)
>>>circle.radius_log
[1, 1]
I do not want the __init__ to reference _radius because it doesn't allow for the ValueErrors to work the way I want them. My code is currently below. What am I missing?
class Circle:
def __init__(self, radius=1):
self.radius_log = [radius]
self.radius = radius
@property
def radius(self):
return s..;@radius.setter
def radius(self, radius):
if(radius < 0):
raise ValueError('Radius cannot be negative!')
self._radius = radius
self.radius_log.append(radius)
@property
def area(self):
return math.pi * self.radius ** 2
@property
def diameter(self):
return self.radius * 2..;@diameter.setter
def diameter(self, diameter):
self.radius = diameter / 2
def __str__(self):
return "Circle of radius" + " " + str(self.radius)
def __repr__(self):
return "Circle(radius=" + str(self.radius) + ")"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started