質問

I want to insert an object into a list and I gives me an error saying:

    Archive.insertdoc(d)
TypeError: insertdoc() missing 1 required positional argument: 'd'

This is in my Main module:

doc = Document(name, author, file)
Archive.insertdoc(doc)

Archive module:

def __init__(self):
    self.listdoc = []

def insertdoc(self, d):
    self.listdoc.append(d)
役に立ちましたか?

解決

You need to create an instance of the Archive class; you are accessing the unbound method instead.

This should work:

archive = Archive()

doc = Document(name, author, file)
archive.insertdoc(doc)

This assumes you have:

class Archive():
    def __init__(self):
        self.listdoc = []

    def insertdoc(self, d):
        self.listdoc.append(d)

If you put two functions at module level instead, you cannot have a self reference in a function and have it bind to the module; functions are not bound to modules.

If your archive is supposed to be a global to your application, create a single instance of the Archive class in the module instead, and use that one instance only.

他のヒント

It looks like Archive.insertdoc is an instance method of the class Archive. Meaning, it must be invoked on an instance of Archive:

doc = Document(name, author, file)
archive = Archive()     # Make an instance of class Archive
archive.insertdoc(doc)  # Invoke the insertdoc method of that instance
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top