[Tutor] dict vs several variables?
d at davea.name
Fri Feb 17 14:45:02 CET 2012
> On 2/17/12, Peter Otten<__peter__ at web.de> wrote:
>> Leam Hall wrote:
>>> I'm building a program that uses one of my own modules for a bunch of
>>> formula defs and another module for the tkinter GUI stuff. There are
>>> half a dozen input variables and about the same in calculated variables.
>>> Is it better/cleaner to just build a global dict and have everything go
>>> into it or pass multiple arguments to each function and have it return
>>> the calculated value?
>> The latter. It makes the dependencies explicit to a reader of the function,
>> it simplifies unit tests, allows it to reuse functions in a different
>> context, and it is more likely to work in a multi-threaded environment.
On 02/17/2012 08:04 AM, leam hall wrote:
> Thanks Peter!
> My concern with variables is that they have to be passed in specific
> order to the function, and they may have to have their type set
> multiple times so that you can perform the right functions on them. In
> a dict you could set it on insert and not have to worry about it.
Please don't top-post. Put your remarks after the parts you're quoting.
You're asking for best-practice, presumably as part of your transition
from small snippets to larger scale. First habit that beginners have to
break is the tendency to keep things in globals. Can it make things
"easier"? Yes, until you try to return to that code and make changes,
and discover that one change affects an unknown number of other parts.
Yes, until you decide you want to use one of those functions in a
multithreaded environment. Yes, until somebody else has to use your code.
Variable can't get their type set. They are bound at any moment to an
object, and that object has a type. Period.
Passed in a specific order? Of course. Very few functions, even binary
ones, are commutative on their arguments. Do you expect
divide(7, 42) to give the same answer as divide(42, 7) ?
You can also use keyword arguments to help disambiguate the order of
arguments. That's especially useful when some arguments are optional.
Real question is whether some (seldom all) of those variables are in
fact part of a larger concept. If so, it makes sense to define a class
for them, and pass around objects of that class. Notice it's not
global, it's still passed as an argument. This can reduce your
parameters from 20 to maybe 6. But make sure that the things the class
represents are really related.
Dictionaries are a built-in collection class, as are lists, sets, and
tuples. But you can write your own. An example of needing a class
might be to hold the coordinates of a point in space. You make a
Location class, instantiate it with three arguments, and use that
instance for functions like
There are times to have arbitrary lists or dictionaries to pass into a
function, and Python has added syntax (*args and **kwargs) to make that
convenient. But the times that is needed are very specialized.
More information about the Tutor