gpt4 book ai didi

python - 类型错误 : unbound method setSize() must be called with Page instance as first argument (got classobj instance instead)

转载 作者:太空宇宙 更新时间:2023-11-03 14:27:53 25 4
gpt4 key购买 nike

当我运行下面提供的代码时,出现以下错误:

类型错误:必须使用 Page 实例作为第一个参数调用未绑定(bind)方法 setSize()(改为使用 int 实例)

In my main, I call a class called Page. The variable pageSize is an integer.

from Page import Page as Page
def main():
#other code....

Page.setSize(pageSize)

This is my class of Page:

class Page:
def __init__(self, frame):
self.frameId = frame
self.pageId = -1
self.timeEvicted = 0
self.timeLoaded = 0
self.ifLoaded = False
self.ownerProcess = 0
self.referencedWords = []

def initalize(owner, pgId, refdWord, ifLoad):
self.ownerProcess = owner
self.pageId = pgId
addRefdWord(refdWord)
self.ifLoaded = ifLoad

def setSize(s):
self.size = s

最佳答案

每个实例方法(可以访问 self 的方法)都必须将 self 作为第一个参数:

def initalize(self, owner, pgId, refdWord, ifLoad):
^^^^^

...

def setSize(self, s):
^^^^^
self.size = s

否则,self 不会在函数体中定义。同样,Page.setSize(pageSize) 将不起作用,因为您没有类的实例。您需要在调用实例方法之前创建一个:

page = Page()
page.setSize(pageSize)

现在,page.size == pageSize

关于python - 类型错误 : unbound method setSize() must be called with Page instance as first argument (got classobj instance instead),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47501366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com