reggaelizard
reggaelizard

Reputation: 831

Semantic bug in rectangle object

I have a class called Rectangle with methods for the rectangle's location, width and height, and I want to write a __str__ method that will nicely print this information out. I've already done a similar thing with my Point class and it worked out just fine, but I think this time it's printing out the memory location of the methods instead of the information converted to a string.

This is my Point class, included here because it's needed to create a Rectangle object:

class Point:
    def __init__(self,initx,inity):
        self.x = initx
        self.y = inity

    def getx(self):
        return self.x

    def gety(self):
        return self.y

    def __str__(self):
        return 'x=' + str(self.x) + ', y=' + str(self.y)

This is Rectangle:

    class Rectangle:
        def __init__(self,lowerleft,awidth,aheight):
            self.location = lowerleft
            self.width = awidth
            self.height = aheight

        def get_width(self):
            return self.width

        def get_height(self):
            return self.height

        def get_location(self):
            return self.location

        def __str__(self):
            return 'Location: ' + str(self.location) + ', width: ' + str(self.get_width) + ', height: ' + str(self.get_height)

And this is the output for the line print(my_rectangle):

Location: x=4, y=5, width: <bound method Rectangle.get_width of <__main__.Rectangle object at 0x1006c4190>>, height: <bound method Rectangle.get_height of <__main__.Rectangle object at 0x1006c4190>>

print(my_rectangle.get_width()), print(my_rectangle.get_height()) and print(my_rectangle.get_location()) worked exactly as expected.

Thanks in advance!

Upvotes: 0

Views: 109

Answers (1)

Hyperboreus
Hyperboreus

Reputation: 32429

You must actually call your member methods:

return 'Location: ' + str(self.location) + ', width: ' + str(self.get_width()) + ', height: ' + str(self.get_height())

Nota bene the parentheses.

Upvotes: 4

Related Questions