Python: Passing variables between functions

Python: Passing variables between functions

This is what is actually happening:

global_list = []

def defineAList():
    local_list = [1,2,3]
    print For checking purposes: in defineAList, list is, local_list 
    return local_list 

def useTheList(passed_list):
    print For checking purposes: in useTheList, list is, passed_list

def main():
    # returned list is ignored
    returned_list = defineAList()   

    # passed_list inside useTheList is set to global_list
    useTheList(global_list) 

main()

This is what you want:

def defineAList():
    local_list = [1,2,3]
    print For checking purposes: in defineAList, list is, local_list 
    return local_list 

def useTheList(passed_list):
    print For checking purposes: in useTheList, list is, passed_list

def main():
    # returned list is ignored
    returned_list = defineAList()   

    # passed_list inside useTheList is set to what is returned from defineAList
    useTheList(returned_list) 

main()

You can even skip the temporary returned_list and pass the returned value directly to useTheList:

def main():
    # passed_list inside useTheList is set to what is returned from defineAList
    useTheList(defineAList()) 

Youre just missing one critical step. You have to explicitly pass the return value in to the second function.

def main():
    l = defineAList()
    useTheList(l)

Alternatively:

def main():
    useTheList(defineAList())

Or (though you shouldnt do this! It might seem nice at first, but globals just cause you grief in the long run.):

l = []

def defineAList():
    global l
    l.extend([1,2,3])

def main():
    global l
    defineAList()
    useTheList(l)

The function returns a value, but it doesnt create the symbol in any sort of global namespace as your code assumes. You have to actually capture the return value in the calling scope and then use it for subsequent operations.

Python: Passing variables between functions

return returns a value. It doesnt matter what name you gave to that value. Returning it just passes it out so that something else can use it. If you want to use it, you have to grab it from outside:

lst = defineAList()
useTheList(lst)

Returning list from inside defineAList doesnt mean make it so the whole rest of the program can use that variable. It means pass the value of this variable out and give the rest of the program one chance to grab it and use it. You need to assign that value to something outside the function in order to make use of it. Also, because of this, there is no need to define your list ahead of time with list = []. Inside defineAList, you create a new list and return it; this list has no relationship to the one you defined with list = [] at the beginning.

Incidentally, I changed your variable name from list to lst. Its not a good idea to use list as a variable name because that is already the name of a built-in Python type. If you make your own variable called list, you wont be able to access the builtin one anymore.

Leave a Reply

Your email address will not be published. Required fields are marked *