Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting error as TypeError: 'function' object is not subscriptable

I have a list in a separate document that resides in a function raw_data() this list is then called by a function in my document data_set().
I am trying to access the list and then index certain items in the following way:

#import function for data_source
from data_source import raw_data 

def data_set():
     ....
     return raw_data() # return the random data set

#function to index certain parts of my list
def data_drawing_loop(data_set):

    #variables for following loop
    rand_data = data_set
    data_heading = data_set[0][2] # error point 
   
    for data_heading in rand_data:
        ...

#my function is then nested within the following function
def visualise_data(data_set):
    data_drawing_loop(data_set)
    ....

#and is finally called via
visualise_data(data_set) 

I have tried replacing def data_drawing_loop(data_set) with (raw_data), defining the variable at the beginning of my code etc.
I’m still not sure what I’m doing wrong, and I keep getting the same issue.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It seems like the issue is with how you’re accessing the list within the data_set function. In particular, when you call rand_data = data_set, you’re not actually accessing the list within data_set, but rather assigning rand_data to be a reference to the same list object as data_set.

Then, when you try to access data_heading = data_set[0][2], you’re actually trying to access the third element of the first element of the data_set function object itself (which is not a list, but rather a function). This is likely causing the error.

To fix this, you should modify your data_drawing_loop function to access the list returned by the data_set function directly, like so:

def data_drawing_loop(data_set):
    rand_data = data_set()
    data_heading = rand_data[0][2]
    heading_tru_fal = False
    heading_as_a_number = 0

    for data_heading in rand_data:
        ...

Here, data_set() calls the data_set function and returns the list, which is then assigned to rand_data. You can then access elements of rand_data using standard list indexing.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading