【发布时间】: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,谢谢你的帮助。
-
不,你需要一些类,但你可能还需要普通函数。
-
发生了很多...尝试实现面向对象编程技术时出现一个奇怪的错误