在python中调用我的类的问题

Dan*_* Jr 0 python inheritance class

我不知道如何保持这个简单......我希望有人看看我的代码然后告诉我为什么我的功能不能正常工作......

我有一节课:

 class PriorityQueue(object):
'''A class that contains several methods dealing with queues.'''

    def __init__(self):
        '''The default constructor for the PriorityQueue class, an empty list.'''
        self.q = []

    def insert(self, number):
        '''Inserts a number into the queue, and then sorts the queue to ensure that the number is in the proper position in the queue.'''
        self.q.append(number)
        self.q.sort()

    def minimum(self):
        '''Returns the minimum number currently in the queue.'''
        return min(self.q)

    def removeMin(self):
        '''Removes and returns the minimum number from the queue.'''
        return self.q.pop(0)

    def __len__(self):
        '''Returns the size of the queue.'''
        return self.q.__len__()

    def __str__(self):
        '''Returns a string representing the queue.'''
        return "{}".format(self.q)

    def __getitem__(self, key):
        '''Takes an index as a parameter and returns the value at the given index.'''
        return self.q[key]

    def __iter__(self):
        return self.q.__iter__()
Run Code Online (Sandbox Code Playgroud)

我有这个函数,它将获取一个文本文件,并通过我的类中的一些方法运行它:

def testQueue(fname):
    infile = open(fname, 'r')
    info = infile.read()
    infile.close()
    info = info.lower()
    lstinfo = info.split()
    queue = PriorityQueue()
    for item in range(len(lstinfo)):
        if lstinfo[item] == "i":
            queue.insert(eval(lstinfo[item + 1]))
        if lstinfo[item] == "s":
            print(queue)
        if lstinfo[item] == "m":
            queue.minimum()
        if lstinfo[item] == "r":
            queue.removeMin()
        if lstinfo[item] == "l":
            len(queue)
        #if lstinfo[item] == "g":
Run Code Online (Sandbox Code Playgroud)

什么对我不起作用的是我打电话给queue.minimumqueue.removeMin().

我彻底难倒了,因为如果我做手工的外壳,所有的工作,当我读了文件,并采取在我的文件从字母说明,但它也可以,但是minimumremoveMin()在外壳将不会显示值,removeMin()但将删除列表中的最低数.

我做错了什么,它没有显示它正在做什么,就像类方法定义的那样?

IE:

 def minimum(self):
     return min(self.q)
Run Code Online (Sandbox Code Playgroud)

当我从我的功能中调用它时,它不应该显示最小值吗?

Ben*_*oyt 6

不,def minimum(self): return min(self.q)在调用时不会显示任何内容.如果你打印输出它只会显示一些东西,如print(queue.minimum()).例外情况是从Python提示符/ REPL执行代码时,默认情况下会打印表达式(除非它们是None).