3
,이 점에 매우를 heres 내 수업 "인스턴스가 ... 어떤 속성이 없다"파이썬 내가 파이썬 몇 가지 문제가
class Rectangle:
def __init__(self, x1=0, y1=0, x2=0, y2=0):
if(x1 > x2):
raise ValueError("x1 cannot be bigger than x2!")
if(y1 > y2):
raise ValueError("y1 cannot be bigger than y2!")
self.pt1 = Point(x1, y1)
self.pt2 = Point(x2, y2)
def __str__(self):
return str("[(" + str(self.pt1.x) + ", " + str(self.pt1.y) + "), (" + str(self.pt2.x) + ", " + str(self.pt2.y) + ")]")
def __repr__(self):
return str("Rectangle(" + str(self.pt1.x) + ", " + str(self.pt1.y) + ", " + str(self.pt2.x) + ", "+ str(self.pt2.y) + ")")
def __eq__(self, other):
return (self.pt1== other.pt1 and self.pt2 == other.pt2)
def __ne__(self, other):
return not self == other
def center(self):
return Point((self.pt2.x - self.pt1.x)/2, (self.pt2.y - self.pt1.y)/2)
와 나는 같은 다른 클래스의 방법 "센터"를 사용하려고 할 때 : 나는 왜 내 방법을 볼 수 없습니다 지금 어떻게 해야할지 몰라
Rectangle instance has no attribute "center".
:
class TestRectangle(unittest.TestCase):
def setUp(self):
self.first = Rectangle(1, 2, 3, 4)
self.second = Rectangle(2, 2, 4, 5)
def test_init(self):
with self.assertRaises(ValueError):
Rectangle(5, 1, 2, 3)
self.assertEqual(self.first.pt1, Point(1, 2))
self.assertEqual(self.first.pt2, Point(3, 4))
self.assertEqual(self.second.pt1.x, 2)
def test_str(self):
self.assertEqual(str(self.first), "[(1, 2), (3, 4)]")
def test_repr(self):
self.assertEqual(repr(self.first), "Rectangle(1, 2, 3, 4)")
def test_eq(self):
self.assertTrue(self.first == Rectangle(1,2,3,4))
self.assertFalse(self.first == self.second)
def test_ne(self):
self.assertFalse(self.first != Rectangle(1,2,3,4))
self.assertTrue(self.first != self.second)
def test_center(self):
self.assertEqual(self.first.center(), Point(2, 2.5))
나는이 메시지를 받았습니다?
완전성을 위해 클래스 '사각형'을 정의한 방법을 포함하도록 첫 번째 코드 세그먼트를 편집해야합니다. – metatoaster
또한,'setUp'은 어떤 assert * 호출도 포함해서는 안됩니다. 'assertRaises'를 자체 테스트 메소드로 옮기는 것을 고려하십시오. – metatoaster
괜찮 았어, 고마워! :) –