Merouane Benthameur Merouane Benthameur - 5 months ago 16
Python Question

Python : 'list' object is not callable

I have this function, trying to loop over a range of length list to get the Ascii letters:

def query_execute (self,query, range):

list_titles = []
list_colunmns_letters = []
cursor.execute(query, range)
description = cursor.description
row = cursor.fetchall()
number_columns = len(description)

for word in description:
list_titles.append(word[0])

for m in range (0,number_columns):

list_colunmns_letters.append(string.ascii_uppercase[m])

return row,list_titles,list_colunmns_letters


It gives me an Error :

for m in range (0,number_columns):
TypeError: 'list' object is not callable


the number_columns variable is an integer, I don't get it why this error, I'm not calling a list .
thanks,

Answer

You used range as a local name by making it an argument to your function:

def query_execute (self,query, range):
#                              ^^^^^

You can't then use the global built-in name as well. Rename the argument to something else; I used params instead:

def query_execute (self, query, params):
    list_titles = []
    list_colunmns_letters = []
    cursor.execute(query, range)
    description = cursor.description
    row = cursor.fetchall()
    number_columns = len(description)

    for word in description:
        list_titles.append(word[0])

    for m in range(0, number_columns):
        list_colunmns_letters.append(string.ascii_uppercase[m])

    return row, list_titles, list_colunmns_letters