On Thu, Nov 6, 2008 at 11:59 PM, yoma <[EMAIL PROTECTED]> wrote: > python version 2.5 in module copy > > we all know that copy have two method: copy() and deepcopy(). > and the explain is > - A shallow copy constructs a new compound object and then (to the > extent possible) inserts *the same objects* into it that the > original contains. > > - A deep copy constructs a new compound object and then, recursively, > inserts *copies* into it of the objects found in the original. > > so i try a example: > import copy > > class A: > i = 1 > > class B: > a = A()
Note that `a` is a class variable, not an instance variable. This ends up being important. > > > b = B() > > x=copy.copy(b) > > y=copy.deepcopy(b) I believe these only copy the instance variables of `b`. They do NOT copy the class `B` (IMHO, copying B would be weird and unexpected behavior here anyway) or its constituent variables, such as `a`. > > print id(x.a), id(b.a) > > print id(y.a), id(y.a) > > the result: > 14505264 14505264 > 14505264 14505264 Thus this makes sense. These all refer to B's variable `a`, which is a class variable and therefore not copied by copy() or deepcopy()-ing `b`, an *instance* of class B. The fact that you can access `a` through B instances does not mean that `a` "belongs" to any instance of B and is merely a result of how Python's object system works. Disclaimer: I am not a CPython dev and did not look at the `copy` module's sources. Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com > > So maybe i have a wrong understand to deep copy and shallow copy or > it is a bug ? > > please help me!! > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list