0

I browsed through several similar questions on here before I decided to ask. There were similar questions about the same error, but they weren't the same type of problem, and none of the answers resolved my problem.

Nonetheless, I'm getting a Typeerror which says 'function' object has no attribute 'getitem'. Here is my code:

def Rectangle(width, height):
  area = Rectangle[width] * Rectangle[height]

a = Rectangle(5, 7)
print(a.area)

Now I know there are easier ways to do this, but the challenge is to do it using functions and classes. I'm wondering where I'm going wrong with this?

1
  • I think you want a class, not a function Commented Oct 31, 2014 at 21:08

1 Answer 1

1

You need to create a class. I'll get you started:

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

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

What you currently have is a function. But seeing as each instance of your object should have it's own attributes, you need to create a type to store them.

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

3 Comments

That makes sense. Thank you. What does the atproperty do?
@HunterWatkins Seeing as area is calculated depending on width and height, we don't want to have it as a constant value. The @property allows us to access it as normal while still keeping it proportional to its other attributes.
Wow, that's useful! Thanks so much. I literally had the first 2 lines of your code written before I posted this, because I figured I had to use a class, but I swore I could do it with just a function. But I was wrong. Thanks again for your help! @AlexThornton

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.