Re: list parameter of a recursive function



TP <Tribulations@xxxxxxxxxxxxxxxxxx> 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 explanation.
The problem with tuples is that it is not easy to modify them: in my case, I
need to append a string to the tuple at each recursive step.
So, it seems to me that I need to write a loop to modify the tuple, because
on a simple example:

a=("foo", "bar")
b=(a[0],a[1],"toto")
b
(u'foo', u'bar', u'toto')

I do not find any other means to obtain that result for b. With lists, I can
use ".extend()".
Am I right?

Yes and no - again. Of course you cannot use extend. But you can
concatenate:

b = a + ("toto",)

Note the trailing comma. A common misconception is to think that tuples
are build using parethesis. They aren't. They are build using the
comma-operator, and needed for disambiguation, e.g. in this case:

foo(a, (b, c))

Diez
.



Relevant Pages

  • Re: list parameter of a recursive function
    ... Diez B. Roggisch wrote: ... costly and more error-prone if you happen to forget to create a copy. ... The problem with tuples is that it is not easy to modify them: ... "When a distinguished but elderly scientist states that something is ...
    (comp.lang.python)
  • Re: Converting records into a text string
    ... Go to www.mvps.org/access and search for "concatenate". ... function there that does the job, though I think you'll have to modify ... John Nurick [Microsoft Access MVP] ... Please respond in the newgroup and not by email. ...
    (microsoft.public.access.queries)
  • Re: list parameter of a recursive function
    ... costly and more error-prone if you happen to forget to create a copy. ... The problem with tuples is that it is not easy to modify them: ... With lists, I can ... s = "toto" ...
    (comp.lang.python)
  • Re: A value of logical expression in C
    ... "Hendrik Schober" wrote: ... while most of other alternatives need to modify the value ... And that's making code more error-prone. ... Alex ...
    (microsoft.public.vc.language)