0

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?

CDspace
  • 2,611
  • 17
  • 32
  • 36
SO03112
  • 168
  • 1
  • 12
  • 1
    `self.create_list_of_files()` – Charles Duffy Apr 17 '17 at 16:34
  • That you got this error suggests you haven't indented your code correctly (You wouldn't have been able to reference `create_list_of_files` without using self). Make sure that `create_list_of_files` is indented to the same level as `__init__`. – Dunes Apr 17 '17 at 16:43
  • 2
    Possible duplicate of [Python call function within class](http://stackoverflow.com/questions/5615648/python-call-function-within-class) – David Cullen Apr 17 '17 at 17:41

3 Answers3

1

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

t = self.create_list_of_files()
Cory Kramer
  • 107,498
  • 14
  • 145
  • 201
0

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

lcastillov
  • 2,135
  • 1
  • 10
  • 17
0

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
Ajax1234
  • 66,333
  • 7
  • 57
  • 95