Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将值插入列表时出错_Python_List_Constructor_Insert - Fatal编程技术网

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