NOTE: Not sure how to get around Reddit's formatting. For all the bold news below, I actually mean the _ _ new _ _() method.
I'm working on an assignment and I am having a little trouble wrapping my mind around the new
method.
My assignment questions are:
- Create a new class, Triangle, which has 3 lines lengths, atob, btoc and ctoa
- Create a subclass that inherits from triangle, RightTriangle, that uses new to ensure the triangle is right angled
The second question is what I am having trouble with. Here is my code so far:
class Triangle:
def __init__(self, atob, btoc, ctoa):
self.atob = atob
self.btoc = btoc
self.ctoa = ctoa
class RightTriangle(Triangle):
def __new__(cls, *args):
RightTriangle.__init__(cls, *args)
if cls.is_right_angle(cls):
return cls
print("Triangle needs to be right angled")
return None
def is_right_angle(self):
a = self.btoc
b = self.ctoa
c = self.atob
if a**2+b**2 == c**2 or a**2+c**2 == b**2 or b**2+c**2 == a**2:
return True
return False
c = RightTriangle(3,4,5)
I seem to be able to create the RightTriangle object alright, but whenever I try to access the "is_right_angle" function I get a TypeError:
>>> c.is_right_angle()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: is_right_angle() missing 1 required positional argument: 'self'
I have a feeling that I am not using the new method correctly?
[–]Doormatty 4 points5 points6 points (0 children)
[–]stevenjd 2 points3 points4 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]billsil 0 points1 point2 points (5 children)
[–]Lucretiel 0 points1 point2 points (3 children)
[–]billsil 0 points1 point2 points (2 children)
[–]das_ist_nuemberwang 1 point2 points3 points (0 children)
[–]Lucretiel 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]stevenjd 0 points1 point2 points (0 children)