Nicolas Vigier wrote: > Hello, > > I have in my python script a function that look like this : > > def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): > if type(arg1) is ListType: > for a in arg1: > my_function(a, arg2, opt1=opt1, opt2=opt2, > opt3=opt3) > return > if type(arg2) is ListType: > for a in arg2: > my_function(arg1, a, opt1=opt1, opt2=opt2, > opt3=opt3) > return > ... then here the real function code > > I'm new to python, so I was wondering if there is a better way to do that. > The reason for a recursive function here is to be able to give lists for > the first 2 args (I could as well use only a simple 'for' without a > recursive call). The problem I have here, is that as I'm working on this > script, I often change the prototype of the function, and each time I > have to think about changing the recursive call too. Is there a way that > I can do a recursive call and say something like "keep all the same > arguments except this one" ?
Here is a non-recursive approach: def tolist(arg): if isinstance(arg, list): return arg return [arg] def f(arg1, arg2, more_args): for arg1 in tolist(arg1): for arg2 in tolist(arg2): # real code If it were my code I would omit the tolist() stuff and instead always pass lists (or iterables) as the function's first two arguments. Peter -- http://mail.python.org/mailman/listinfo/python-list