list parameter of a recursive function

TP Tribulations at Paralleles.invalid
Wed Oct 6 17:00:10 EDT 2010


Chris Torek wrote:

>>import copy from copy
> 
> [from copy import copy, rather]

Yes, sorry.

> Note that if f() is *supposed* to be able to modify its second
> parameter under some conditions, you would want to make the copy
> not at the top of f() but rather further in, and in this case,
> that would be trivial:
> 
>         def f(arg, some_list = None):
>             if some_list is None:
>                 some_list = []
>             ...
>             if some_condition:
>                 # make copy of list and append something
>                 f(new_arg, some_list + [elem])
>             elif other_condition:
>                 # continue modifying same list
>                 f(new_arg, some_list)

Thanks a lot Chris!
I think I prefer doing an explicit copy.copy, because it allows to remind 
the reader that it is very important to take care of that. But your trick is 
very interesting also.

Cheers,

Julien


-- 
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.\
9&1+,\'Z4(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)



More information about the Python-list mailing list