Search code examples
pythonpython-3.xpylint

Using variable before assigment error when using lists


Using variable 'criteria_names' before assignment pylint(E0601). This error pops up only for the 'criteria_names' list which is already assigned, but not for the other lists, which are assigned the same way. Any pointers or solutions are very appreciated!

CODE SNIPPET:

data = list()
lines = list()
n_vectors = list()
vectors = list()
medium_vectors = list()
classnames = list()
classes_insert_lines = list()
vectors_raw = list()
criteria_names = list()

def DataGet(temp_pulse_file):
    data[:] = []
    n_vectors[:] = []
    vectors[:] = []
    medium_vectors[:] = []
    classnames[:] = []
    classes_insert_lines[:] = []
    vectors_raw[:] = []
    criteria_names[:] = [] #<- this is where the error pops up

    with open(temp_pulse_file, "r", encoding='utf-8') as file:
        lines = (file.read()).split("\n")
    file.close()

    n = -1
    for i in range(len(lines)):
        data.append(lines[i].split(","))

        if data[i][0] == ">":
            n += 1
            classnames.append(data[i][1])
            classes_insert_lines.append(i + 1)
            n_vectors.append(0)
            vectors_raw.append([])
            vectors.append([])
        elif data[i][0] == "#":
            weights = data[i][1:]
        elif data[i][0] == "$":
            criteria_names = list(data[i][1:])
        else:
            n_vectors[n] += 1
            vectors[n].append(data[i])
            vectors_raw[n].append(lines[i])


    n_elems = len(vectors[0][0])
    classes = len(classnames)

    for i in range(classes):
        for j in range(n_vectors[i]):
            for k in range(n_elems):
                vectors[i][j][k] = float(vectors[i][j][k])

    return n_elems, classes, n_vectors, weights, lines, classes_insert_lines, vectors_raw

n_elems, classes, n_vectors, weights, lines, classes_insert_lines, vectors_raw = DataGet(getcurrentdir() + 'db.pulse')
print(' criteria_names:',criteria_names)

When I remove criteria_names[:] = [], no errors whatsoever for the other lists used in the function. Anything helps.


Solution

  • Thanks to @martineau and @Barmar, added global criteria_names at the beginning of the DataGet() function and it worked like a miracle. Weird tho.