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' )
from
adirectory_of_modules
, you canimport
aspecific_module.py
specific_module.py
, can contain aClass
withsome_methods()
or justfunctions()
specific_module.py
, you can instantiate aClass
or callfunctions()
Class
, you can executesome_method()
Example:
Excerpts from PEP 8 - Style Guide for Python Code:
The problem is in the
import
line. You are importing a module, not a class. Assuming your file is namedother_file.py
(unlike java, again, there is no such rule as "one class, one file"):if your file is named findTheRange too, following java's convenions, then you should write
you can also import it just like you did with
random
: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:
c) You can call methods in the same way you do in java:
d) You can use built in function, and the huge python library:
e) If you still want to use a class, and you don't need
self
, you can use@staticmethod
: