簡體   English   中英

如何修復此 AttributeError

[英]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.currentNone ,它沒有這樣的屬性job_ID

要解決您的問題,您必須在打印q.current.job_ID之前確保q.current不是None 除此之外我無法給你任何幫助,因為我不知道你的程序的目的是什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM