recursive function call

2005-11-08 Thread Nicolas Vigier
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 ?

Thanks

Nicolas

-- 
http://n0x.org/ - [EMAIL PROTECTED]


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: recursive function call

2005-11-08 Thread Nicolas Vigier
Peter Otten said:
 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.

Hmm, that's right. After all it's not very useful to be able to give
a single element, passing a list all the time is more simple. That's
what I'll do. I got the idea of passing a list or a singe element
because that's what they do in SCons, but in my case it's not really
usefull.

Thanks

-- 
http://n0x.org/ - [EMAIL PROTECTED]


-- 
http://mail.python.org/mailman/listinfo/python-list