0

Ive only just started using classes in python and so cant figure out how to fix this presumably simple error. I have a class that essentially collates multiple csv files, sorts and cleans the data, and outputs a finished list, looks like below:

class CollateCSV():
    def __init__(self, input_dir="", file_list="", output=False):
        # initialises some global variables

    def read_file_list(self):
        #reads a csv file of already "dealt with" files
        return x #list of filenames

    def list_files_to_read(self, show=False, perm=True):
        #gives list of csv files yet to be cleaned - the ones the program will actually import and read
        return files_to_read


    def sort_list(self, unsorted_list):
        #sorts a list of OrderedDict by multiple keys
        return sorted_list

    def import_sort(self):
        #reads each CSV file, cleans it, passes it to sort_list function and returns cleaned & sorted list of OrderedDict
        return sort_list(x)

    def return_final_list(self):
        #does some things relating to logging if self.output == True. But used mainly as single easy place to return the final sorted & cleaned list of OrderedDict
        return import_sort()

The way I have setup my project, this code is in a seperate file to my main code, and so Ive imported this as a module. As such I know the code itself works as when running the below code Ive got errors that Ive fixed indicating missing variables or syntax errors etc throughout the above code.

from convert import CollateCSV
x = CollateCSV("./csv_files", "./file_list.csv", output=False)

print(x.list_files_to_reas())

The problem I have is when I run the above code know I get an eror saying:

for file in list_files_to_read(show=False, perm=True):
NameError: name 'list_files_to_read' is not defined

Ive tried moving the functions within the CollateCSV class around, and attempted to do the second piece of code in the same file as the first, but still get the same error. Could someone impart some advice on what Im doing wrong with classes in python? Thanks.

user2757598
  • 59
  • 2
  • 3
  • 1
    To call a method from another method, you need to use `self`. Instead of `list_files_to_read(show=False, perm=True)` do `self.list_files_to_read(show=False, perm=True)`. – Patrick Haugh Aug 08 '18 at 22:08
  • @PatrickHaugh Do I need to add .self to all of the functions within the class? – user2757598 Aug 08 '18 at 22:19
  • @user2757598 you need to add `self` to access **any attribute** – juanpa.arrivillaga Aug 08 '18 at 22:22
  • Why when functions are within a class do you need to add self. before calling them, what does requiring that particulalry do? – user2757598 Aug 08 '18 at 22:24
  • @user2757598 it's how Python scoping/namespacing works. It's pretty simple, those functions are in the classes namespace. To access them, you must access the classes namespace. You can either do that directly through the class, `FooClass.method(foo_instance, arg1, arg2)` (don't do this) or from the instance: `foo_instance.method(arg1, arg2)` (do this). A method is merely a function in the classes namespace, a fact of which the function has no knowledge. That is why it requires `self` (the instance) as a parameter. This is done implicitly in some languages, e.g. Java, but not in Python. – juanpa.arrivillaga Aug 08 '18 at 22:26
  • @user2757598 note, there is nothing magical about the name `self`, it is merely convention, you could use `this` or `banana`, the only special thing about it is that it is the *first positional argument*, now, something implicit does happen, which is that this argument gets *implicitly* passed the instance when you access the method from the instance. This actually isn't magical, it just so happens that functions in Python are *descriptors* who's `__get__` method returns a new function which has already abound the instance... this is a bit of advanced Python stuff though. – juanpa.arrivillaga Aug 08 '18 at 22:41
  • @user2757598 Appreciated. – user2757598 Aug 08 '18 at 22:56

2 Answers2

0

in your for loop you need to write

for file in x.list_files_to_read(show=False, perm=True):
    # ...

since x is your instance of CollatesCSV

N Chauhan
  • 3,407
  • 2
  • 7
  • 21
-2

You're probably missing the __init__.py file in the directory containing your class file.

What is __init__.py for?

Daniel Scott
  • 7,418
  • 5
  • 39
  • 58