Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'module' object is not callable - calling method in another file

I have a fair background in java, trying to learn python. I'm running into a problem understanding how to access methods from other classes when they're in different files. I keep getting module object is not callable.

I made a simple function to find the largest and smallest integer in a list in one file, and want to access those functions in another class in another file.

Any help is appreciated, thanks.

class findTheRange():      def findLargest(self, _list):         candidate = _list[0]         for i in _list:             if i > candidate:                 candidate = i         return candidate      def findSmallest(self, _list):         candidate = _list[0]         for i in _list:             if i < candidate:                 candidate = i         return candidate 

 import random  import findTheRange   class Driver():       numberOne = random.randint(0, 100)       numberTwo = random.randint(0,100)       numberThree = random.randint(0,100)       numberFour = random.randint(0,100)       numberFive = random.randint(0,100)       randomList = [numberOne, numberTwo, numberThree, numberFour, numberFive]       operator = findTheRange()       largestInList = findTheRange.findLargest(operator, randomList)       smallestInList = findTheRange.findSmallest(operator, randomList)       print(largestInList, 'is the largest number in the list', smallestInList, 'is the                smallest number in the list' ) 
like image 916
Sox Keep You Warm Avatar asked May 27 '13 21:05

Sox Keep You Warm


People also ask

How do you fix a module object is not callable?

The Python "TypeError: 'module' object is not callable" occurs when we import a module as import some_module but try to call it as a function or class. To solve the error, use dot notation to access the specific function or class before calling it, e.g. module. my_func() .

What is module object not callable?

It says module object is not callable, because your code is calling a module object. A module object is the type of thing you get when you import a module. What you were trying to do is to call a class object within the module object that happens to have the same name as the module that contains it.

How do you call a module object in Python?

You can use the functions such as factorial(), floor() and fabs() within this module. But if you try using a function with name math(), the compiler will be confused. It will throw an error called TypeError 'module' object is not callable in Python.

How do I fix int object is not callable in Python?

But in Python, this would lead to the Typeerror: int object is not callable error. To fix this error, you need to let Python know you want to multiply the number outside the parentheses with the sum of the numbers inside the parentheses. Python allows you to specify any arithmetic sign before the opening parenthesis.


1 Answers

The problem is in the import line. You are importing a module, not a class. Assuming your file is named other_file.py (unlike java, again, there is no such rule as "one class, one file"):

from other_file import findTheRange 

if your file is named findTheRange too, following java's convenions, then you should write

from findTheRange import findTheRange 

you can also import it just like you did with random:

import findTheRange operator = findTheRange.findTheRange() 

Some other comments:

a) @Daniel Roseman is right. You do not need classes here at all. Python encourages procedural programming (when it fits, of course)

b) You can build the list directly:

  randomList = [random.randint(0, 100) for i in range(5)] 

c) You can call methods in the same way you do in java:

largestInList = operator.findLargest(randomList) smallestInList = operator.findSmallest(randomList) 

d) You can use built in function, and the huge python library:

largestInList = max(randomList) smallestInList = min(randomList) 

e) If you still want to use a class, and you don't need self, you can use @staticmethod:

class findTheRange():     @staticmethod     def findLargest(_list):         #stuff... 
like image 61
Elazar Avatar answered Sep 20 '22 10:09

Elazar