[Python-talk] Calling functions with variable number of arguments
bruce.labitt at autoliv.com
bruce.labitt at autoliv.com
Wed Sep 2 15:25:09 EDT 2009
Lloyd Kvam <python at venix.com> wrote on 09/01/2009 05:23:41 PM:
> On Tue, 2009-09-01 at 13:05 -0400, bruce.labitt at autoliv.com wrote:
> > Say one has three functions, function 1 has one input argument,
function 2
> > has 2 input arguments and function 3 has no input arguments.
> >
> > I have made a function list, and an argument list
> >
> > def func1(n):
> > print 'Function '+str(n)+' ran'
> >
> > def func2( n, a ):
> > print 'Function '+str(n)+' ran '+str(a)+'times'
> >
> > def func3:
> > print 'Function 3 ran'
> >
> > funclist = [func1, func2, func3]
> > arglist = [ a, (b,c), None ]
> >
> > I'd like to execute something like this (which does not work)
> >
> > for item, arg in zip( funclist, arglist ):
> > item( arg )
> >
> > If I run this, it dies on the call to func2, giving a TypeError
Exception.
> > So I use *arg. This only works for func2, but fails for func1 with
> > TypeError because "argument after * must be a sequence"
>
> To get past this hurdle, define arglist as:
> arglist = [ [a], (b,c), [] ]
>
> Now the *arg notation will work for each call.
> >
> > I can get func1 & func3 to work if I modify the function func3 from
above
> > to
> >
> > def func3(a):
> > print 'Function 3 ran'
> >
> >
> > So is there a way to get all three functions to work using a similar
(or
> > any) construction?
>
> There is still likely to be a hurdle. You need to create a new arglist
> every time a, b, or c changes. In other words, the a in arglist (unless
> a is mutable) is determined when arglist is created. Changing a
> variable named a elsewhere in your program has no impact on arglist[0].
> These are not memory references as in C.
>
> Possibly you have a case like:
>
> def do_stuff( a, b, c, d, e):
> # now you could create arglist here
> arglist = [ [a], (b,c), [] ]
> # but you are probably better off doing this
> def use_func1():
> return func1( a)
> def use_func2():
> return func2( b, c)
> def use_func3():
> return func3()
> funclist = [use_func1, use_func2, use_func3]
> # but this is all pretty wordy and you probably do not really
> care
> # about these function names so
> funclist = [
> lambda : func1(a),
> lambda : func2( b,c),
> lambda : func3(),
> ]
> # and you no longer have explicit args for the funclist
> functions
> # instead the args are picked up from the local scope.
> for f in funclist:
> f()
>
> One last comment. If you are pushed into an older Python version or if
> the lambdas are being created within a loop where a, b, and c are all
> varying, and then executed outside the loop, you might need to code the
> funclist functions like this:
> lambda a=a: func1(a) # parameter a defaults to the local scope
> variable a
> # the duplication of names is odd looking and not required
> lambda b=b,c=c: func2( b, c)
>
> You still do not provide arguments in your func calls because the
> defaults are built into the definitions.
>
> >
Lloyd, thank you so much. I have learned an awful lot from the list.
-Bruce
******************************
Neither the footer nor anything else in this E-mail is intended to or constitutes an <br>electronic signature and/or legally binding agreement in the absence of an <br>express statement or Autoliv policy and/or procedure to the contrary.<br>This E-mail and any attachments hereto are Autoliv property and may contain legally <br>privileged, confidential and/or proprietary information.<br>The recipient of this E-mail is prohibited from distributing, copying, forwarding or in any way <br>disseminating any material contained within this E-mail without prior written <br>permission from the author. If you receive this E-mail in error, please <br>immediately notify the author and delete this E-mail. Autoliv disclaims all <br>responsibility and liability for the consequences of any person who fails to <br>abide by the terms herein. <br>
******************************
More information about the Python-talk
mailing list