1

I am struggling to pass a list (holder, which contains [0,0]) to another function. I want check_neighbours to pick up the list and then do some further processing. I think I am passing it correctly in main() but I am not sure what I must do to get the list to simply display its contents when called upon inside check_neighbours. I have tried a = holder but I received the error:

global name "holder" is not defined

I believe I must put holder = #something here but I cannot figure out what.

 def create_matrix(file):
    with open('network.txt') as f:
        Alist = []
        for line in f:
            part = []
            for x in line.split(','):
                part.append(int(x))
            Alist.append(part)
    return Alist

def start_node(Alist):
        i=0
        j=0
        #point node to pos [0][0] of Alist
        node = Alist[i][j]
        #create a list to hold co-ordinates
        holder = []
        holder.append(i)
        holder.append(j)
        print holder

        return node, holder

#test neighbours to see if they can be used
def check_neighbours(node, Alist):
        holder = #something 



#code begins here
def main():
        file = ("F:/media/KINGSTON/Networking/network.txt")
        Alist = create_matrix(file)
        node = start_node(Alist)
        holder = check_neighbours(node, Alist)
main()

1 Answer 1

4

At the end of start_node, you do this:

return node, holder

… but when you call it, you do this:

node = start_node(Alist)

That means your local node variable ends up as a tuple of node, holder. You don't want that. You want this:

node, holder = start_node(Alist)

Meanwhile, you say "I think I am passing it correctly in the main() function", but you're not passing it at all in the main function:

holder = check_neighbours(node, Alist)

There's no way for check_neighbours to get holder here, because you're not giving it holder here.

Plus, when you defined check_neighbours, you did this:

def check_neighbours(node, Alist):
    holder = #something 

This doesn't take a holder parameter, it defines a new local variable named holder. So, change those two lines to:

def check_neighbours(node, Alist, holder):

And call it with:

holder = check_neighbours(node, Alist, holder)

Also, note that you're assigning the result of check_neighbors back to the holder variable, replacing whatever you passed in. Often, that's a perfectly reasonable thing to do (think my_name = my_name.replace('Joseph', 'Joe')), but make sure it really is what you want (and, of course, make sure that check_neighbors ends by doing a return holder, or returning some other appropriate value).

Finally, as I said in your other question: If you keep using the same name for your parameters and the values you pass in, you're going to keep confusing yourself like this. If you used different names, it would be much easier for you to see what you're missing. Even something as simple as a prefix on each one:

def main():
    file = ("F:/media/KINGSTON/Networking/network.txt")
    my_list = create_matrix(file)
    my_node, my_holder = start_node(my_list)
    my_new_holder = check_neighbours(my_node, my_list, my_holder)
Sign up to request clarification or add additional context in comments.

3 Comments

thank you again, I am making quite a few assumptions about how variable passing works but I appreciate your help greatly
@bigl: Here's the simple rule of thumb: Variable passing is always completely explicit. If you have def foo(x, y, z):, and it ends with return v, w, you will always need to call it with 3 values, and store the result in 2, like a, b = foo(c, d, e). (This rule of thumb is modified in more complicated cases, like classes with methods, global variables, etc., but ignore those for now.)
thank you for that, very useful information. everything is working great now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.