[英]How to fix this AttributeError
我正在尝试访问类中函数内的变量并打印它。 每当我尝试时,我都会收到错误消息:AttributeError: 'NoneType' object has no attribute '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)
错误来自此代码中的最后一个打印语句。
这是此处使用的类中的函数:
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
。 因此,您收到AttributeError
的原因是因为q.current
是None
,它没有这样的属性job_ID
。
要解决您的问题,您必须在打印q.current.job_ID
之前确保q.current
不是None
。 除此之外我无法给你任何帮助,因为我不知道你的程序的目的是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.