2013-05-27 19 views
24

我在Java中有一個公平的背景,試圖學習python。我遇到了一個問題,就是當它們處於不同的文件中時,如何從其他類訪問方法。我一直在獲取模塊對象不可調用。'module'對象不可調用 - 在另一個文件中調用方法

我做了一個簡單的函數來查找一個文件列表中的最大和最小整數,並且想要在另一個文件的另一個類中訪問這些函數。

任何幫助表示讚賞,謝謝。

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

Python不是Java。這些代碼沒有任何理由在課堂上。使它們成爲模塊級功能。 –

+0

在一個更復雜的python程序中,我不需要我在類中組織所有的方法,我仍然在考慮java,感謝您的幫助。 –

+5

不,你將需要一些類,但你也可能需要普通的功能。 – Elazar

回答

49

的問題是在import線。您正在導入模塊,而不是類。假設你的文件被命名爲other_file.py(與Java不同,再次,沒有「一個階級,一個文件」的規則):

from other_file import findTheRange 

,如果你的文件被命名爲findTheRange過,下面的Java的convenions,那麼你應該寫

from findTheRange import findTheRange 

你也可以導入它就像你用random做:

import findTheRange 
operator = findTheRange.findTheRange() 

其他一些意見:

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)如果ST生病要使用一個類,你不需要self,你可以使用@staticmethod

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

這兩行與OP的代碼相同。更改後的導入可修復錯誤,但使用OP的文件名(並解釋明顯的冗餘)可能會有所幫助。 – delnan

+0

他們不*相同* - 這是一個動態的調用,而不是一個靜態的調用。 (請糾正我,如果我錯了) – Elazar

+0

,我不知道* OP的文件名是什麼 - 我只能猜測它確實是'findTheRange',因爲這是Java中的約定。無論如何,歡迎您提高我的答案。 – Elazar

1

我想從我的經驗分享一些成果,與那些誰可能有同樣的懷疑。

  • from一個directory_of_modules,你可以import一個specific_module.py
  • 這個specific_module.py,可以從specific_module.py包含Classsome_methods()或只是functions()
  • ,您可以實例從這個Class或撥打functions()
  • 您可以執行some_method()

例子:it will keep the order over the files of your application and it will give a better visibility of what exactly is being imported.

模塊應該是短期和所有:

#!/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」 公約名稱。

  • 功能:名稱以小寫字母,單詞由下劃線分隔。

  • 全局變量:約定與函數約定相同。

相關問題