Re: list parameter of a recursive function

2010-10-07 Thread TP
Seebs wrote: > On 2010-10-07, TP wrote: >> Diez B. Roggisch wrote: >>> A safer alternative for these cases is using tuples, because they are >>> immutable. > >> The problem with tuples is that it is not easy to modify them: > > This is probably the best post-and-response I've seen in the last c

Re: list parameter of a recursive function

2010-10-07 Thread Terry Reedy
On 10/6/2010 3:22 PM, TP wrote: Hi, I have a function f that calls itself recursively. It has a list as second argument, with default argument equal to None (and not [], as indicated at: http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) This sort of function is an exception. I

Re: list parameter of a recursive function

2010-10-07 Thread Diez B. Roggisch
TP writes: > Diez B. Roggisch wrote: > >> Back to your example: your solution is perfectly fine, although a bit >> costly and more error-prone if you happen to forget to create a copy. >> A safer alternative for these cases is using tuples, because they are >> immutable. > > Thanks Diez for your

Re: list parameter of a recursive function

2010-10-07 Thread Chris Rebert
On Wed, Oct 6, 2010 at 10:25 PM, TP wrote: > Diez B. Roggisch wrote: > >> Back to your example: your solution is perfectly fine, although a bit >> costly and more error-prone if you happen to forget to create a copy. >> A safer alternative for these cases is using tuples, because they are >> immut

Re: list parameter of a recursive function

2010-10-06 Thread Seebs
On 2010-10-07, TP wrote: > Diez B. Roggisch wrote: >> A safer alternative for these cases is using tuples, because they are >> immutable. > The problem with tuples is that it is not easy to modify them: This is probably the best post-and-response I've seen in the last couple of months. -s -- C

Re: list parameter of a recursive function

2010-10-06 Thread TP
Steven D'Aprano wrote: >> 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. > > I think a comment is better for that. It's better to explicitly say > something is important than to assume the reader will guess.

Re: list parameter of a recursive function

2010-10-06 Thread TP
Diez B. Roggisch wrote: > Back to your example: your solution is perfectly fine, although a bit > costly and more error-prone if you happen to forget to create a copy. > A safer alternative for these cases is using tuples, because they are > immutable. Thanks Diez for your explanation. The proble

Re: list parameter of a recursive function

2010-10-06 Thread Steven D'Aprano
On Wed, 06 Oct 2010 23:00:10 +0200, TP wrote: > 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. I think a comment is better for that. It's better to explicitly say something is important than to assume the re

Re: list parameter of a recursive function

2010-10-06 Thread Diez B. Roggisch
TP writes: > Hi, > > I have a function f that calls itself recursively. It has a list as second > argument, with default argument equal to None (and not [], as indicated at: > http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) > > This is the outline of my function: > > def f ( a

Re: list parameter of a recursive function

2010-10-06 Thread TP
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

Re: list parameter of a recursive function

2010-10-06 Thread Chris Torek
In article TP wrote: >I have a function f that calls itself recursively. It has a list as second >argument, with default argument equal to None (and not [], as indicated at: >http://www.ferg.org/projects/python_gotchas.html#contents_item_6 ) > >This is the outline of my function: > >def f ( arg