2

This is my code:

class Rectangle(object):
    def __init__(self, height, width):
        self.height = height
        self.width = width

    def __str__(self):
        return '{} x {} = {}'.format(self.height, self.width, self.area)

    def area(self):
        self.area=self.height*self.width
        return self.area


def primarySchool(height, width):
    return str(Rectangle(height, width))

For input height=7 and width=4 the output is

>>> primarySchool(7, 4):
7 x 4 = <bound method _runjcbjp.<locals>.Rectangle.area of
<__main__._runjcbjp.<locals>.Rectangle object at 0x2b482cd637f0>> 

instead of 7 x 4 = 28.

How can I fix this?

3 Answers 3

3

In your Rectangle class, the area member is defined as a method. As a consequence, print(self.area) will give you the representation of that method object, which is that <...> thing.

What you want is the result of the area method, not the method itself. Therefore, you need to call the method, by writing parentheses after its name.

Your code should be:

return '{} x {} = {}'.format(self.height, self.width, self.area())

Additionnally, be careful not to reassign the same name in your method. In your area method, you write:

self.area = self.height * self.width

As a consequence, after the first call to instance.area(), the area member will be overwritten, from a function to a number. Subsequent calls would thus fail, with something like "Int object is not callable".

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

5 Comments

Careful here though ... On issuing that statement again, things will probably fail because the area method reassigns self.area (which is generally a bad idea)
@mgilson Oh. That's so uncommon I did not even see it. Editing my answer right away.
class Rectangle(object): def __init__(self, height, width): self.height = height self.width = width def __str__(self): return '{} x {} = {}'.format(self.height, self.width, self.area) def __init__(self,height, width): self.height = height self.width = width self.area=self.height*self.width
i wrote a new init after str
@AvoAsatryan There's no point in writing to __init__ in a same class; the second would overwrite the first.
2

area is a method of your class, so you have to call it to get the return value (and not the method itself).

But given the fact that you assign to self.area inside the method it seems like you want it as "cached" property (accessible without calling it explicitly):

class Rectangle(object):
    def __init__(self, height, width):
        self.height = height
        self.width = width

    def __str__(self):
        return '{} x {} = {}'.format(self.height, self.width, self.area)

    @property
    def area(self):   # cached version
        try:
            return self._area
        except AttributeError:
            self._area=self.height*self.width
        return self._area


def primarySchool(height, width):
    return str(Rectangle(height, width))

primarySchool(7, 4)
# '7 x 4 = 28'

Or as uncached version:

class Rectangle(object):
    def __init__(self, height, width):
        self.height = height
        self.width = width

    def __str__(self):
        return '{} x {} = {}'.format(self.height, self.width, self.area)

    @property
    def area(self):
        return self.height*self.width

Or just calculate it in the __init__ and also set it as attribute:

class Rectangle(object):
    def __init__(self, height, width):
        self.height = height
        self.width = width
        self.area = height * width

    def __str__(self):
        return '{} x {} = {}'.format(self.height, self.width, self.area)

3 Comments

def __init__(self,height, width): self.height = height self.width = width self.area=self.height*self.width
this is more siple solution
Depends on your use-case. If you want area to be dynamic (e.g. it should update when you change width) you should make it a property or method :)
1

You're trying to have both a function and property called "area".

Why not simply:

def area(self):
    return self.height*self.width

Call with:

self.area()

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.