ir6
ir6

Reputation: 95

"'module' object is not callable" error when creating a class in python

I main java and just started on python, and I ran into this error when I was trying to create a class. Can anyone tell me what is wrong?

import rectangle

a = rectangle(4, 5)

print(a.getArea())

this is what is in the rectangle class:

class rectangle:
    l = 0
    w = 0

    def __init__(self, l, w):
        self.l = l
        self.w = w

    def getArea(self):
        return self.l * self.w

    def getLength(self):
        return self.l

    def getWidth(self):
        return self.w

    def __str__(self):
        return "this is a", self.l, "by", self.w, "rectangle with an area of", self.getArea()

Upvotes: 0

Views: 62

Answers (2)

35308
35308

Reputation: 652

your gonna need to specify what module the function is from so either import all the functions so change import rectangle to from rectangle import *

or switch the rectangle(4,5) to rectangle.rectangle(4,5)

Upvotes: 0

Marc-Alexandru Baetica
Marc-Alexandru Baetica

Reputation: 360

I don't know what you have implemented in the rectangle module but I suspect that what you're actually looking for is this:

from rectangle import rectangle
a = rectangle(4, 5)
print(a.getArea())

If not, give us an indication of what's in rectangle.py

Upvotes: 2

Related Questions