python中如何解决类互相调用问题_在python中调用我的类的问题

在Python中遇到类方法互相调用的问题,主要涉及PriorityQueue类的实现和testQueue函数。PriorityQueue类包含insert、minimum、removeMin等方法,testQueue函数读取文件并根据指令操作队列。问题出在minimum和removeMin调用时不显示预期结果。虽然在shell中手动操作或按文件指令操作时,removeMin能正确移除最小值,但minimum方法不显示最小值。可能是由于输出未正确处理导致。
摘要由CSDN通过智能技术生成

我不知道如何保持这种简单...我希望有人也看看我的代码,并告诉我为什么我的一个函数不能像它应该那样工作......

我有一堂课:

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__()我有这个函数需要一个文本文件,并通过我的类中的一些方法运行它:

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":不适合我的是我对queue.minimum和queue.removeMin()的电话。

我完全困惑,因为如果我在shell中手动执行它,这一切都有效,当我正在阅读文件并从我的文件中的字母中获取指令时,它也可以工作,但minimum和removeMin()不会显示但是,removeMin()将从列表中移除最小的数字。

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

IE:

def minimum(self):

return min(self.q)当我从我的功能调用它时不应该显示最小值?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值