Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass lots of variables to and from a function in Python?

I do scientific programming, and often want to show users prompts and variable pairs, let them edit the variables, and then do the calulations with the new variables. I do this so often, that I wrote a wxPython class to move this code out of the main program. You set up a list for each variable with the type of the variable (string, float, int), the prompt, and the variable's current value. You then place all of these lists in one big list, and my utility creates a neatly formated wxPython panel with prompts and the current values which can be edited.

When I started, I only had a few variables, so I would write out each variable.

s='this is a string'; i=1; f=3.14
my_list=[ ['s','your string here',s], ['i','your int here',i], ['f','your float here'],]
input_panel = Input(my_list)

 # the rest of the window is created, the input_panel is added to the window, the user is
 # allowed to make  choices, and control returns when the user hits the calculate button

s,i,f = input_panel.results()     # the .results() function returns the values in a list

Now I want to use this routine for a lot of variables (10-30), and this approach is breaking down. I can create the input list to the function over multiple lines using the list.append() statements. When the code returns from the function, though, I get this huge list that needs to be unpacked into the right variables. This is difficult to manage, and it looks like it will be easy to get the input list and output list out of sync. And worse than that, it looks kludgy.

What is the best way to pass lots of variables to a function in Python with extra information so that they can be edited, and then get the variables back so that I can use them in the rest of the program?

If I could pass the variables by reference into the function, then users could change them or not, and I would use the values once the program returned from the function. I would only need to build the input list over multiple lines, and there wouldn't be any possiblity of the input list getting out of sync with the output list. But Python doesn't allow this.

Should I break the big lists into smaller lists that then get combined into big lists for passing into and out of the functions? Or does this just add more places to make errors?

like image 381
Curt Avatar asked May 26 '09 20:05

Curt


People also ask

Does Python allows you to pass multiple arguments to a function?

Python allows you to pass multiple arguments to a function.

How does Python pass variables to functions?

Python passes arguments by assignment. That is, when you call a Python function, each function argument becomes a variable to which the passed value is assigned.

How do you pass arbitrary number of arguments to a function in Python?

Well, using *args in a function is Python's way to tell that this one will: Accept an arbitrary number of arguments.


2 Answers

The simplest thing to do would be to create a class. Instead of dealing with a list of variables, the class will have attributes. Then you just use a single instance of the class.

like image 178
Ned Batchelder Avatar answered Oct 03 '22 14:10

Ned Batchelder


There are two decent options that come to mind.

The first is to use a dictionary to gather all the variables in one place:

d = {}
d['var1'] = [1,2,3]
d['var2'] = 'asdf'
foo(d)

The second is to use a class to bundle all the arguments. This could be something as simple as:

class Foo(object):
    pass
f = Foo()
f.var1 = [1,2,3]
f.var2 = 'asdf'
foo(f)

In this case I would prefer the class over the dictionary, simply because you could eventually provide a definition for the class to make its use clearer or to provide methods that handle some of the packing and unpacking work.

like image 34
Doug Avatar answered Oct 03 '22 15:10

Doug