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,639
  • 18
  • 30
  • 36
SO03112
  • 178
  • 2
  • 12
  • 1
    `self.create_list_of_files()` <- `self` is the implicit first argument. – 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) –  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
  • 114,268
  • 16
  • 167
  • 218
0

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

lcastillov
  • 2,163
  • 1
  • 11
  • 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
  • 69,937
  • 8
  • 61
  • 102