Passing a variable number of arguments to a function

mercado python.dev.9 at gmail.com
Thu Feb 12 18:44:45 EST 2009


I have the following piece of code that is bugging me:

#-------------------------------------------------------------------------------
def someFunc(arg1, arg2=True, arg3=0):
    print arg1, arg2, arg3

someTuple = (
    ("this is a string",),
    ("this is another string", False),
    ("this is another string", False, 100)
)

for argList in someTuple:
    if len(argList) == 1:
        someFunc(argList[0])
    elif len(argList) == 2:
        someFunc(argList[0], argList[1])
    elif len(argList) == 3:
        someFunc(argList[0], argList[1], argList[2])
#-------------------------------------------------------------------------------

Is it possible to rewrite this code so I don't have that awkward if
statement at the bottom that passes every variation in the number of
arguments to the function?  I know that it's possible to define a function
to accept a variable number of parameters using *args or **kwargs, but it's
not possible for me to redefine this function in the particular project I'm
working on.

What I would really like to do is something like this (pseudocode) and do
away with the if statement completely:

#-------------------------------------------------------------------------------
def someFunc(arg1, arg2=True, arg3=0):
    print arg1, arg2, arg3

someTuple = (
    ("this is a string",),
    ("this is another string", False),
    ("this is another string", False, 100)
)

for argList in someTuple:
    someFunc(expandArgList(argList))
#-------------------------------------------------------------------------------

Is this possible?  Thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090212/85514066/attachment.html>


More information about the Python-list mailing list