Python 将值插入列表时出错
我想在列表中插入一个对象,但出现了一个错误,错误是:Python 将值插入列表时出错,python,list,constructor,insert,Python,List,Constructor,Insert,我想在列表中插入一个对象,但出现了一个错误,错误是: Archive.insertdoc(d) TypeError: insertdoc() missing 1 required positional argument: 'd' 这是我的主要模块: doc = Document(name, author, file) Archive.insertdoc(doc) 存档模块: def __init__(self): self.listdoc = [] def insertdo
Archive.insertdoc(d)
TypeError: insertdoc() missing 1 required positional argument: 'd'
这是我的主要模块:
doc = Document(name, author, file)
Archive.insertdoc(doc)
存档
模块:
def __init__(self):
self.listdoc = []
def insertdoc(self, d):
self.listdoc.append(d)
您需要创建归档
类的实例;您正在访问未绑定的方法
这应该起作用:
archive = Archive()
doc = Document(name, author, file)
archive.insertdoc(doc)
这假设您有:
class Archive():
def __init__(self):
self.listdoc = []
def insertdoc(self, d):
self.listdoc.append(d)
如果将两个函数放在模块级,则不能在函数中有一个self
引用并将其绑定到模块;函数不绑定到模块
如果您的存档应该是应用程序的全局存档,请在模块中创建存档
类的单个实例,并仅使用该实例。您需要创建存档
类的实例;您正在访问未绑定的方法
这应该起作用:
archive = Archive()
doc = Document(name, author, file)
archive.insertdoc(doc)
这假设您有:
class Archive():
def __init__(self):
self.listdoc = []
def insertdoc(self, d):
self.listdoc.append(d)
如果将两个函数放在模块级,则不能在函数中有一个self
引用并将其绑定到模块;函数不绑定到模块
如果您的存档应该是应用程序的全局存档,请在模块中创建
存档
类的单个实例,并仅使用该实例。它看起来像存档。insertdoc
是类存档
的实例方法。也就是说,它必须在存档的实例上调用:
doc = Document(name, author, file)
archive = Archive() # Make an instance of class Archive
archive.insertdoc(doc) # Invoke the insertdoc method of that instance
它看起来像是Archive.insertdoc
是类Archive
的一个实例方法。也就是说,它必须在存档的实例上调用:
doc = Document(name, author, file)
archive = Archive() # Make an instance of class Archive
archive.insertdoc(doc) # Invoke the insertdoc method of that instance