Python 如何修复此AttributeError
我试图访问类中函数中的变量并打印它。每当我尝试时,总是会出现错误:AttributeError:“NoneType”对象没有属性“job\u ID”Python 如何修复此AttributeError,python,oop,Python,Oop,我试图访问类中函数中的变量并打印它。每当我尝试时,总是会出现错误:AttributeError:“NoneType”对象没有属性“job\u ID” def driver(): q = my_queue.Queue_() for line in df: if 'received' in line: q.enqueue(line) print("Adding job " + q.new_item.job_ID + "
def driver():
q = my_queue.Queue_()
for line in df:
if 'received' in line:
q.enqueue(line)
print("Adding job " + q.new_item.job_ID + " to the queue with the timestamp: " + q.new_item.time_stamp + ".")
print("The prority of the job is: " + q.new_item.job_priority)
print("The job type is: " + q.new_item.job_type)
if 'respond' in line:
q.dequeue()
print("Completed job " + q.current.job_ID + " in " + str(int(q.time_elapsed)) + " seconds.")
if 'active' in line:
q.active_jobs()
print("Total number of jobs: " + str(len(q.temp)))
print("Average priority: " + str(q.average))
if 'modify' in line:
q.modify(line)
print("Modified job " + q.current.job_ID)
此错误来自此代码中的最后一个print语句
这是此处使用的类中的函数:
def modify(self, x): # need to fix printing bug
self.current = self.head
while self.current != None:
if x[1] in self.current.get_data():
self.current.data[2] = x[2]
self.current.data[3] = x[3]
break
# print("Modified job " + current.job_ID)
else:
# print('The job details cannot be modified.')
pass
self.current = self.current.get_next()
您提供的
modify
函数中循环的退出条件是self.current==None
在最后一条条件语句中调用modify()
时:
if 'modify' in line:
q.modify(line) // here
print("Modified job " + q.current.job_ID)
您正在将q.current
计算为None
。因此,您获得属性错误的原因是q.current
是None
,它没有称为job\u ID
的属性
要解决问题,必须确保打印q.current.job\u ID
之前q.current
不是None
。除此之外,我无法为您提供任何帮助,因为我不知道您程序的用途。您提供的modify
函数中循环的退出条件是self.current==None
在最后一条条件语句中调用modify()
时:
if 'modify' in line:
q.modify(line) // here
print("Modified job " + q.current.job_ID)
您正在将q.current
计算为None
。因此,您获得属性错误的原因是q.current
是None
,它没有称为job\u ID
的属性
要解决问题,必须确保打印q.current.job\u ID
之前q.current
不是None
。除此之外,我无法为您提供任何帮助,因为我不知道您的程序的用途。while循环的条件是while self.current!=无
,这就是为什么我们知道当modify()
完成运行时,self.current
是None
。由于您正在对变量q
调用modify()
方法,q.current
将是None
。由于没有任何代码行明确地将self.current
计算为None
,因此它必须是。get\u next()
使while循环具有条件while self.current!=None的方法无
,这就是为什么我们知道当modify()
完成运行时,self.current
是None
。由于您正在对变量q
调用modify()
方法,q.current
将是None
。由于没有任何代码行明确地将self.current
计算为None
,因此必须是使self.current==None
的方法