[Python-talk] Calling functions with variable number of arguments

bruce.labitt at autoliv.com bruce.labitt at autoliv.com
Tue Sep 1 13:05:03 EDT 2009


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"

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?

Thanks!
Bruce

Bruce Labitt
Autoliv Electronics
1011B Pawtucket Blvd, PO Box 1858
Lowell, MA  01853

Email: bruce.labitt at autoliv.com. 
Tel:  (978) 674-6526
Fax: (978) 674-6581 

******************************
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