Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Passing variables between functions

I've spent the past few hours reading around in here and elsewhere, as well as experimenting, but I'm not really understanding what I am sure is a very basic concept: passing values (as variables) between different functions.

For example, I assign a whole bunch of values to a list in one function, then want to use that list in another function later:

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

Based on my understanding of what function arguments do, I would expect this to do as follows:

  1. Initialize 'list' as an empty list; call main (this, at least, I know I've got right...)
  2. Within defineAList(), assign certain values into the list; then pass the new list back into main()
  3. Within main(), call useTheList(list)
  4. Since 'list' is included in the parameters of the useTheList function, I would expect that useTheList would now use the list as defined by defineAList(), NOT the empty list defined before calling main.

However, this is obviously a faulty understanding. My output is:

For checking purposes: in defineAList, list is ['1', '2', '3'] For checking purposes: in useTheList, list is [] 

So, since "return" obviously does not do what I think it does, or at least it does not do it the way I think it should... what does it actually do? Could you please show me, using this example, what I would have to do to take the list from defineAList() and use it within useTheList()? I tend to understand things better when I see them happening, but a lot of the examples of proper argument-passing I've seen also use code I'm not familiar with yet, and in the process of figuring out what's going on, I'm not really getting a handle on this concept. I'm using 2.7.

ETA- in the past, asking a similar question, it was suggested that I use a global variable instead of just locals. If it will be relevant here also- for the purposes of the class I'm taking, we're not permitted to use globals.

Thank you!

like image 420
user2113818 Avatar asked Apr 16 '13 17:04

user2113818


People also ask

How do you pass variables between files in Python?

To use global variables between files in Python, we can use the global keyword to define a global variable in a module file. Then we can import the module in another module and reference the global variable directly. We import the settings and subfile modules in main.py . Then we call settings.

How do you pass an argument variable in Python?

As you expect it, Python has also its own way of passing variable-length keyword arguments (or named arguments): this is achieved by using the **kwargs symbol. When using **kwargs, all the keywords arguments you pass to the function are packed inside a dictionary.


2 Answers

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())  
like image 187
Pavel Anossov Avatar answered Sep 18 '22 15:09

Pavel Anossov


You're 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 shouldn't 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 doesn't 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.

like image 42
Silas Ray Avatar answered Sep 17 '22 15:09

Silas Ray