Python “模块”对象不可调用 - 在另一个文件中调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16780510/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'module' object is not callable - calling method in another file
提问by Sox Keep You Warm
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.
我在 Java 方面有一定的背景,正在尝试学习 Python。我在理解如何从其他类访问不同文件中的方法时遇到问题。我不断收到模块对象不可调用。
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' )
采纳答案by Elazar
The problem is in the importline. 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"):
问题出import在线路上。您正在导入一个模块,而不是一个类。假设您的文件已命名other_file.py(与 java 不同,同样,没有“一类,一个文件”这样的规则):
from other_file import findTheRange
if your file is named findTheRange too, following java's convenions, then you should write
如果你的文件也被命名为 findTheRange,遵循 java 的约定,那么你应该写
from findTheRange import findTheRange
you can also import it just like you did with random:
您也可以像导入一样导入它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)
a) @Daniel Roseman 是对的。你根本不需要在这里上课。Python 鼓励过程式编程(当然,当它适合时)
b) You can build the list directly:
b) 您可以直接构建列表:
  randomList = [random.randint(0, 100) for i in range(5)]
c) You can call methods in the same way you do in java:
c) 您可以像在 Java 中一样调用方法:
largestInList = operator.findLargest(randomList)
smallestInList = operator.findSmallest(randomList)
d) You can use built in function, and the huge python library:
d) 您可以使用内置函数和庞大的 python 库:
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:
e) 如果你仍然想使用一个类,而你不需要self,你可以使用@staticmethod:
class findTheRange():
    @staticmethod
    def findLargest(_list):
        #stuff...
回答by ivanleoncz
- froma- directory_of_modules, you can- importa- specific_module.py
- this specific_module.py, can contain aClasswithsome_methods()or justfunctions()
- from a specific_module.py, you can instantiate aClassor callfunctions()
- from this Class, you can executesome_method()
- from一- directory_of_modules,你可以- import一- specific_module.py
- this specific_module.py,可以包含Classwithsome_methods()或 justfunctions()
- 从 a specific_module.py,您可以实例化 aClass或调用functions()
- 从此Class,您可以执行some_method()
Example:
例子:
#!/usr/bin/python3
from directory_of_modules import specific_module
instance = specific_module.DbConnect("username","password")
instance.login()
Excerpts from PEP 8 - Style Guide for Python Code:
Modules should have short and all-lowercase names.
Notice:Underscores can be used in the module name if it improves readability.
A Python module is simply a source file(*.py), which can expose:
Class:names using the "CapWords" convention.
Function:names in lowercase, words separated by underscores.
Global Variables:the conventions are about the same as those for Functions.
模块应具有简短且全小写的名称。
注意:如果可以提高可读性,可以在模块名称中使用下划线。
Python 模块只是一个源文件 (*.py),它可以公开:
类:使用“CapWords”约定的名称。
功能:名称小写,单词用下划线分隔。
全局变量:约定与函数的约定大致相同。

