Pnelson
Pnelson

Reputation: 57

Error insert a value into a list-Python

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)

Upvotes: 0

Views: 65

Answers (2)

user2555451
user2555451

Reputation:

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

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1122142

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.

Upvotes: 2

Related Questions