Home > Software engineering >  "'module' object is not callable" error when creating a class in python
"'module' object is not callable" error when creating a class in python

Time:11-05

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

CodePudding user response:

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

CodePudding user response:

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)

  • Related