我在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')
Python不是Java。這些代碼沒有任何理由在課堂上。使它們成爲模塊級功能。 –
在一個更復雜的python程序中,我不需要我在類中組織所有的方法,我仍然在考慮java,感謝您的幫助。 –
不,你將需要一些類,但你也可能需要普通的功能。 – Elazar