【问题标题】:'module' object is not callable - calling method in another file“模块”对象不可调用 - 在另一个文件中调用方法
【发布时间】:2013-05-22 17:50:09
【问题描述】:

我有良好的java背景,正在尝试学习python。当其他类位于不同的文件中时,我在理解如何访问其他类的方法时遇到了问题。我不断收到模块对象不可调用。

我做了一个简单的函数来查找一个文件的列表中的最大和最小整数,并希望在另一个文件的另一个类中访问这些函数。

感谢任何帮助,谢谢。

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

【问题讨论】:

  • Python 不是 Java。这段代码没有任何理由属于一个类。使它们成为模块级函数。
  • 在一个更复杂的python程序中,我不是需要我所有的方法都组织在类中,我还在考虑java,谢谢你的帮助。
  • 不,你需要一些类,但你可能还需要普通函数。
  • 发生了很多...尝试实现面向对象编程技术时出现一个奇怪的错误

标签: python import module


【解决方案1】:

问题出在import 行。您正在导入一个模块,而不是一个类。假设您的文件名为 other_file.py(与 java 不同,同样没有“一个类,一个文件”这样的规则):

from other_file import findTheRange

如果你的文件也被命名为 findTheRange,遵循 java 的约定,那么你应该写

from findTheRange import findTheRange

您也可以像使用 random 一样导入它:

import findTheRange
operator = findTheRange.findTheRange()

其他一些cmets:

a) @Daniel Roseman 是对的。你根本不需要在这里上课。 Python 鼓励过程式编程(当然,在合适的时候)

b) 您可以直接构建列表:

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

c) 你可以像在 java 中一样调用方法:

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

d) 您可以使用内置函数和庞大的 python 库:

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

e) 如果你还想使用某个类,并且不需要self,可以使用@staticmethod

class findTheRange():
    @staticmethod
    def findLargest(_list):
        #stuff...

【讨论】:

  • 这两行与 OP 的代码相同。更改后的导入修复了错误,但使用 OP 的文件名(并解释明显的冗余)可能会有所帮助。
  • 它们不是相同的 - 这是一个动态调用而不是静态调用。 (如有错误请指正)
  • 我不知道 OP 的文件名是什么 - 我只猜它确实是 findTheRange,因为这是 Java 中的约定。无论如何,欢迎您改进我的答案。
  • 你说得对,线条不相同,我是过失。但是,猜测模块名称为 findTheRange 是非常安全的(它是导入的,就像是类一样使用,因为您注意到它是 Java 约定)。
  • 应该说明导入是相对于当前文件的。我还要澄清一下,如果处理混合了Packages 和Folders 的结构,from &lt;directory&gt; import &lt;file&gt; 通常会变成from &lt;pseudo_dir&gt; import &lt;file&gt;。 PyCharm 在其“项目资源管理器”中很好地表示了这一点。
【解决方案2】:
  • from一个directory_of_modules,你可以import一个specific_module.py
  • 这个specific_module.py,可以包含Classsome_methods() 或只是functions()
  • specific_module.py,您可以实例化Class 或调用functions()
  • 从这个Class,你可以执行some_method()

示例:

#!/usr/bin/python3
from directory_of_modules import specific_module
instance = specific_module.DbConnect("username","password")
instance.login()

摘自PEP 8 - Style Guide for Python Code

模块应该有简短的全小写名称。

注意:如果提高可读性,可以在模块名称中使用下划线。

Python 模块只是一个源文件(*.py),它可以暴露:

  • 类:使用“CapWords”约定的名称。

  • 功能:小写名称,下划线分隔的单词。

  • 全局变量:约定与函数的约定大致相同。

【讨论】:

  • 不能从特定模块导入类吗?
猜你喜欢
  • 1970-01-01
  • 2019-07-14
  • 2016-11-23
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 2012-09-28
相关资源
最近更新 更多