shallow vs deep copying [was Re: Applescript-users Digest, Vol 2, Issue 492]
shallow vs deep copying [was Re: Applescript-users Digest, Vol 2, Issue 492]
- Subject: shallow vs deep copying [was Re: Applescript-users Digest, Vol 2, Issue 492]
- From: has <email@hidden>
- Date: Thu, 28 Jul 2005 12:32:29 +0100
Ryan Wilcox wrote:
> >No it doesn't. It performs a deep copy of the list object, so any
>>mutable items within that list will also be duplicated. To shallow
>>copy a list, you have to use 'items of <list>'.
>
>What is involved in the shallow copy?
This stuff is identical to Python, fyi (AS's 'copy' = Python's copy.deepcopy). I think you're confusing yourself with misconceived test code and misobserved results. Observe:
set a to {1}
set b to {2}
set myList to {a, b} -- a list of mutable items
set myList2 to myList -- myList2 contains same list (included for comparison)
set shallowCopy to items of myList -- shallowCopy contains new list with same contents
copy myList to deepCopy -- deepCopy contains new list with duplicate contents
set item 1 of a to 99 -- modify content of a sub-list
log myList --> {{99}, {2}}
log myList2 --> {{99}, {2}}
log shallowCopy --> {{99}, {2}}
log deepCopy --> {{1}, {2}} -- note, no change
set item 1 of myList to 0 -- modify content of main list
log myList --> {0, {2}}
log myList2 --> {0, {2}}
log shallowCopy --> {{99}, {2}} -- note, no change
log deepCopy --> {{1}, {2}}
Exactly what you'd expect.
has
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden