how to take variable from different `def()` function to another - python
Question: I have two different functions like :

    
    
    def messageWindow():
        # all the necessary operations
        feature_matrix_db = zip( B_mean , G_mean , R_mean, cont_list , ene_list , homo_list , cor_list, dis_list)
        return feature_matrix_db
    
    def open():
        #all the necessary operations
        feature_matrix_ip = zip( B_mean1 , G_mean1 , R_mean1, cont_list1 , ene_list1 , homo_list1 , cor_list1 , dis_list1)
        return feature_matrix_ip
    
    def result():
        COLUMNS = 12
        image_count = 0
        resultlist_key = []
        result_list = list()
        i = 0
        a_list = list()
        b_list = list()
        a_list.append(feature_matrix_ip)
        while i < 70:
            b_list.append(feature_matrix_db[i])
            dist = distance.euclidean(a_list,b_list[i])
            result_list.append(dist)
            resultlist_key = OrderedDict(sorted(enumerate(result_list),key=lambda x:x[0])).keys()
            i = i + 1 
    
        res_lst_srt = {'values': result_list,'keys':resultlist_key}
        res_lst_srt['values'], res_lst_srt['keys'] = zip(*sorted(zip(res_lst_srt['values'], res_lst_srt['keys'])))
        key = res_lst_srt['keys']
    
        for i1,val in enumerate(key):
            if i1 < 4:
                image_count += 1
                r, c = divmod(image_count, COLUMNS)
                im = Image.open(resizelist[val])
                tkimage = ImageTk.PhotoImage(resized)
                myvar = Label(win, image=tkimage)
                myvar.image = tkimage
                myvar.grid(row=r, column=c)
    

First two `def()` function will return `feature_matrix_db` and
`feature_matrix_ip`, and I want these results to be imported into the next
`def()` function **result**. And it is giving an error like :

    
    
    im = Image.open(resizelist[val])
    File "E:\Canopy\System\lib\site-packages\PIL\Image.py", line 1956, in open
    prefix = fp.read(16)
    AttributeError: 'numpy.ndarray' object has no attribute 'read'
    

Any suggestions are welcome. Thanks in advance!


Answer: You probably want to read up about namespacing and scoping in python. There's
some info here: <https://docs.python.org/2/tutorial/classes.html>

For example your `messageWindow()` function:

    
    
    def messageWindow():
        # all the necessary operations
        feature_matrix_db = zip( B_mean , G_mean , R_mean, cont_list , ene_list , homo_list , cor_list, dis_list)
        return feature_matrix_db
    

But where are `B_mean`, `G_mean`, `R_mean`, `cont_list`, `ene_list`,
`homo_list`, `cor_list` and `dis_list` defined? You can pass them to
`messageWindow()` as arguments:

    
    
    def messageWindow(B_mean , G_mean , R_mean, cont_list , ene_list , homo_list , cor_list, dis_list):
        # all the necessary operations
        feature_matrix_db = zip( B_mean , G_mean , R_mean, cont_list , ene_list , homo_list , cor_list, dis_list)
        return feature_matrix_db
    

or assign some value to them within the function, or assign some value to them
outside of the function using the `global` keyword. But you can't just call
them without having ever said what values they take.

Let's look at the first few lines of `result()`:

    
    
    def result():
        COLUMNS = 12
        image_count = 0
        resultlist_key = []
        result_list = list()
        i = 0
        a_list = list()
        b_list = list()
        a_list.append(feature_matrix_ip)
    

Again, where is `feature_matrix_ip` defined? What you could do instead is
this:

    
    
    feature_matrix_ip = open() #super bad idea to call your function open()
    a_list.append(feature_matrix_ip)
    

