SO03112
SO03112

Reputation: 178

Error in function in class - takes exactly 1 argument

I have a class where I pass a list of documents, and in a method, it creates a list of those documents:

class Copy(object):
   def __init__(self, files_to_copy):
      self.files_to_copy = files_to_copy

Here, it creates a list of files:

def create_list_of_files(self):
    mylist = []
    with open(self.files_to_copy) as stream:
        for line in stream:
            mylist.append(line.strip())
    return mylist

Now, I try to access the method in another method in the class:

def copy_files(self):
    t = create_list_of_files()
    for i in t:
        print i

Then I run the following under if __name__ == "__main__":

a = Copy()
a.copy_files()

This throws:

TypeError: create_list_of_files() takes exactly 1 argument (0 given)

Am I using the method wrong?

Upvotes: 0

Views: 538

Answers (3)

Ajax1234
Ajax1234

Reputation: 71451

You are not passing any variable to the class. In your init method, your code states that init takes one variable, files_to_copy. You need to pass the variable that stores the correct information. For instance:

class Copy(object):
   def __init__(self, files_to_copy):
       self.files_to_copy = files_to_copy

#need to pass something like this:
a = Copy(the_specific_variable)
#now, can access the methods in the class

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 117876

You need to call the method off self, which is the "1 argument" the method is looking for

t = self.create_list_of_files()

Upvotes: 1

lcastillov
lcastillov

Reputation: 2153

You need to call create_list_of_files as follow: self.create_list_of_files()

Upvotes: 0

Related Questions