3

I have some problem with Python, so heres my class to this point:

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)

and when I try to use method "center" in another class like that:

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))

I got this message:

Rectangle instance has no attribute "center". 

I dunno what to do now, why don't it see my method?

7
  • 2
    You should edit your first code segment to include how you defined your class Rectangle for completeness. Commented Nov 30, 2016 at 1:13
  • Also, setUp shouldn't contain any assert* calls. Consider moving the assertRaises to its own test method. Commented Nov 30, 2016 at 1:15
  • Ok, fixed, thanks! :) Commented Nov 30, 2016 at 1:20
  • You should ensure the methods are properly indented; center may be a function defined in the module alongside the class Rectangle, instead of a method of that class. Commented Nov 30, 2016 at 1:25
  • How can I fix my indents in notepad++? Commented Nov 30, 2016 at 1:35

1 Answer 1

4

Reading your code, it appears you define center outside of the Rectangle class, because of an indentation error.

Therefore, Rectangle instances do not have any center method.

I tried with the right indentation, and it did work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.