On Tue Sep 30 11:32:41 CEST 2008, Steven D'Aprano
>On Tue, 30 Sep 2008 08:58:15 +0000, John O'Hagan wrote:Thanks, both to you and Bruno for pointing this out, I'll certainly be using
>Hi Pythonistas,
I'm looking for the best way to pass an arbitrary number and type of
variables created by one function to another. They can't be global
because they may have different values each time they are used in the
second function.
So far I'm trying to do something like this:
def process_args([list, of, command-line, arguments] ):
If you are trying to process commandline args, I suggest you don't re-
invent the wheel. Have a look at the two standard modules, optparse and
getopt. Of the two, getopt is probably simpler to get started with, but
optparse is more powerful.
it in future.
>To answer your more general question, see below.[...snip code example...]
> do stuffEr, yes. Presumably main_function actually does something. So it expects
return {dictionary : of, local : variables }
def main_function( **kwargs ):
do stuff
return result
kw1 = process_args( [some, list] )
kw2 = process_args( [a, different, list] )
for i in main_function( **kw1 ):
kw2[ var1 ] = i
kw2[ var2 ] = len( i )
for j in main_function(* *kw2):
print j
This only seems to work if I specify a default value for every possible
parameter of main_function and also for any others which may be passed
to it, which is a bit tedious because there are very many of them but
only a few are used in any given execution of the program.
some arguments, and if the arguments aren't given, then it will fail. To
prevent it failing when arguments aren't given, they must have default
values. So define them, once, and be done with it:
>Default values are a feature, not a problem to be solved.I take your point, but in this case all necessary parameters are present in
the keyword dictionary, so I guess I was surprised that default values still
need to specified.
Also, the function body tests for the presence of each parameter and only acts
on those that are there, so it does not fail because of a missing argument.
However, I'm sure there's a good reason that it works this way.
>As for your nested for-loops (see above), I'm guessing that you're trying[...]
to copy items from one dictionary kw1 to another kw2, before processing
kw2. You should check out the update method on dictionaries.
That's also of great use, thank you.
What I'm actually trying to do, though, is add some of the output of one call
of main_function to the arguments of another call of the same function (with
different arguments). It's a long story, but I have a number-crunching
function which produces lists of numbers representing musical notes, either
rhythmic or melodic, to be printed out as a score using Lilypond and/or
played by samples or synthesizers using sox.
The melodic and rhythmic parts work by themselves, but I'm trying to get the
function to feed itself rhythms for its melodies, and vice-versa.
I guess that's already too much information....
Thanks for your help.
John