属性错误。无法访问Class中的属性

时间:2017-11-28 18:17:34

标签: python class attributes

我正在尝试在Python的main函数中调用类Pager。当我运行程序时,它给了我一个错误:

Traceback (most recent call last):   File "lab4.py", line 113, in <module>
    main()   File "lab4.py", line 34, in main
    demandPager.processRef(p, clock, rand)   File "/Users/kiranbhimani/Desktop/OSLab4/Pager.py", line 16, in processRef
    desiredPage = Pager.frameTable.findPageById(testId, process.getProcessId()) 

**AttributeError: class Pager has no attribute 'frameTable'**

如何访问frameTable?如果我插入“self”作为参数,我就无法调用该类。它说processRef需要4个参数,但只有3个参数。

我不确定这里发生了什么。提前谢谢!

class Pager:
def __init__(self, machineSize, pageSize):
    self.machineSize = machineSize
    self.pageSize = pageSize
    self.frameTable = FrameTable(int(machineSize/pageSize))

@staticmethod 
def processRef(process, clock, randreader):
    ref = int(process.currentReference)
    testId = int(ref / Page.size)
    #print Pager.machineSize
    desiredPage = Pager.frameTable.findPageById(testId, process.getProcessId())
    if (isHit(desiredPage, process)):       
        desiredPage.addRefdWord(ref)

    else:
        if (not frameTable.isFull()):
            frameTable.addPage(process, testId, ref)

        else:
            pageToEvict = findPageToReplace(replacementAlgo, randreader)
            frameTable.evictPage(pageToEvict)
            frameTable.addPage(process, testId, ref)
            desiredPage = frameTable.lastPageAdded

        desiredPage = frameTable.lastPageAdded
        desiredPage.setIfLoaded(true)
        process.incrNumPageFaults()


    desiredPage.timeLastUsed = clock
    frameTable.updateTimes()

这是主要功能:

from Process import Process
from Page import Page as Page
from Pager import Pager
from FrameTable import FrameTable 
import sys

runningProcesses = []
finishedProcesses = []
def main():
    #read input
    machineSize = int(sys.argv[1])
    pageSize = int(sys.argv[2])
    processSize = int(sys.argv[3])
    jobMix = int(sys.argv[4])
    numOfRefPerProcess = int(sys.argv[5])
    replacementAlgo = (sys.argv[6])
    demandPager = Pager(machineSize, pageSize)
    Page.size = pageSize
    Process.size = processSize
    setProc(jobMix)
    demandPager.replacementAlgo = replacementAlgo

    index = 0
    clock = 0
    while(len(runningProcesses) > 0):
        p = runningProcesses[index]
        for i in range(3):
            demandPager.processRef(p, clock, rand)
            p.setCurrentReference(p.calcNextReference(rand))
            p.incrRefsMade()

            clock+=1

            if (p.getRefsMade() == numRefPerProcess):
                finishedProcesses.add(p)
                runningProcesses.remove(p)
                index-=1
                break
        if (index == numProcesses-1):
            index = 0
        else:
            index+=1
    print "printing....."
    printOutput(args)

1 个答案:

答案 0 :(得分:0)

您尝试访问类属性frameTable,但此类根本没有添加属性。该类的对象具有machineSizeFrameSizepageTable的属性 - 这是您实例化的每个对象的属性之一。

例如,一旦你点击了创建命令就会有一个demandPager.frameTable,但是你没有给Pager作为一个类的任何属性(除了内置命令)。

也许你想使用self

desiredPage = self.frameTable.findPageById(testId, process.getProcessId())