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 trialAndrew McLane
3,385 Pointsdef area(self):
So when I just return self.length * self.width, the code works. However, when I assign self.area to the area calculation, the code doesn't work. Was wondering why this is?
class Rectangle:
def __init__(self, width, length):
self.width = width
self.length = length
@property
def area(self):
self.area = self.width * self.length
return self.area
1 Answer
Clayton Perszyk
Treehouse Moderator 48,850 PointsHi Andrew,
You can't have an attribute with the same name as a property / method. if you change self.area to something like self._area, it will work. However, you should declare the attribute (self._area) in the initializer.
class Rectangle:
def __init__(self, width, length):
self.width = width
self.length = length
self._area = None
@property
def area(self):
self._area = self.width * self.length
return self._area
Andrew McLane
3,385 PointsAndrew McLane
3,385 Pointsthanks!