From: bart.c on
Benjamin Kaplan wrote:
> On Thu, Jun 17, 2010 at 4:20 PM, bart.c <bartc(a)freeuk.com> wrote:

>> I don't know how Python does things, but an object should either
>> specify a special way of duplicating itself, or lend itself to some
>> standard way of doing so. (So for a list, it's just a question of
>> copying the data in the list, then recursively duplicating each new
>> element..)

> It's the recursively duplicating each element that's the problem. How
> do you know when to stop?

When you reach a primitive object (one not comprising other objects). (I
don't know if Python allows circular references, but that would give
problems anyway: how would you even print out such a list?)

--
Bartc

From: Lie Ryan on
On 06/18/10 20:00, bart.c wrote:
> (I
> don't know if Python allows circular references, but that would give
> problems anyway: how would you even print out such a list?)


Python uses ellipsis to indicate recursive list:

>>> a = [1, 2, 3]
>>> a.append(a)
>>> a
[1, 2, 3, [...]]
From: bart.c on
Lie Ryan wrote:
> On 06/18/10 20:00, bart.c wrote:
>> (I
>> don't know if Python allows circular references, but that would give
>> problems anyway: how would you even print out such a list?)
>
>
> Python uses ellipsis to indicate recursive list:
>
>>>> a = [1, 2, 3]
>>>> a.append(a)
>>>> a
> [1, 2, 3, [...]]

Ok, perhaps whatever logic print uses to know when to stop and show "...",
can also be used by copy handlers...

(Although I have an issue with the way that that append works. I tried it in
another, simpler language (which always does deep copies):

L:=(1,2,3)
L append:= L
print L

output: (1,2,3,(1,2,3))

which is exactly what I'd expect, and not (1,2,3,(1,2,3,(1,2,3,...))) )

--
bartc

From: Steven D'Aprano on
On Fri, 18 Jun 2010 12:07:38 +0100, bart.c wrote:

> (Although I have an issue with the way that that append works. I tried
> it in another, simpler language (which always does deep copies):
>
> L:=(1,2,3)
> L append:= L
> print L
>
> output: (1,2,3,(1,2,3))
>
> which is exactly what I'd expect,
> and not (1,2,3,(1,2,3,(1,2,3,...))) )

I find that behaviour a big surprise. You asked to append the list L, not
a copy of the list L. So why is this "simpler" language making a copy
without being asked?

If you asked for:

L:=(1,2,3)
M:=(0,1)
M append:= L

does it also append a copy of L instead of L? If so, how do you append
the original rather than wastefully making a copy? If L is huge, making a
copy before appending will be slow, and potentially fail.



--
Steven