Re: How best to pass arbitrary parameters from one function to another
- From: Bruno Desthuilliers <bruno.42.desthuilliers@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 30 Sep 2008 11:26:36 +0200
John O'Hagan a écrit :
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] ):
do stuff
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.
If this is about commmand line arguments parsing and defaults, you may want to have a look at the optparse package in the stdlib.
Also, kwargs work fine with default arguments too, ie:
def func(arg1=1, arg2='yadda', arg3=None):
print arg1, arg2, arg3
for kw in ({}, {'arg1':42}, {'arg2':'yop', 'arg3' : range(5)}):
func(**kw)
HTH
.
- References:
- How best to pass arbitrary parameters from one function to another
- From: John O'Hagan
- How best to pass arbitrary parameters from one function to another
- Prev by Date: Re: Problems compiling on CentOS Python 2.5
- Next by Date: Re: How best to pass arbitrary parameters from one function to another
- Previous by thread: How best to pass arbitrary parameters from one function to another
- Next by thread: Re: How best to pass arbitrary parameters from one function to another
- Index(es):