[Tutor] Deep Copy

Timothy M. Brauch tbrauch@tbrauch.com
Mon, 15 Apr 2002 21:22:15 -0400


I guess I should read the documentation before I send this things off.

I just read about copy.deepcopy.  That will solve my problems

Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE Fork 0.8 -- press F1 for help
>>> import copy
>>> t = [1, 2, 3]
>>> p = copy.deepcopy(t)
>>> p.append(4)
>>> p
[1, 2, 3, 4]
>>> t
[1, 2, 3]
>>> id(t)
21712336
>>> id(p)
21740864

Yeah, that is what I need.

 - Tim

----- Original Message -----
From: "Timothy M. Brauch" <tbrauch@tbrauch.com>
To: <tutor@python.org>
Sent: Monday, April 15, 2002 9:16 PM
Subject: [Tutor] Deep Copy


> Is there a way to force Python to do a deep copy?  What I do not want is
as
> follows:
>
> Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> IDLE Fork 0.8 -- press F1 for help
> >>> t = [1, 2, 3]
> >>> p = t
> >>> id(t)
> 10717008
> >>> id(p)
> 10717008
> >>> p.append(4)
> >>> p
> [1, 2, 3, 4]
> >>> t
> [1, 2, 3, 4]
>
> I know I could:
>
> Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> IDLE Fork 0.8 -- press F1 for help
> >>> t = [1, 2, 3]
> >>> p=[]
> >>> for i in t:
>  p.append(i)
>
>
> >>> p
> [1, 2, 3]
> >>> t
> [1, 2, 3]
> >>> p.append(4)
> >>> p
> [1, 2, 3, 4]
> >>> t
> [1, 2, 3]
> >>> id(t)
> 10836352
> >>> id(p)
> 10838048
>
> But, I would prefer not doing that every time (I am doing this alot).
>
>  - Tim
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor