By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,585 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,585 IT Pros & Developers. It's quick & easy.

is it a bug in Module copy or i am wrong??

P: n/a
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()
b = B()

x=copy.copy(b)

y=copy.deepcopy(b)

print id(x.a), id(b.a)

print id(y.a), id(y.a)

the result:
14505264 14505264
14505264 14505264

So maybe i have a wrong understand to deep copy and shallow copy or
it is a bug ?

please help me!!

Nov 7 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, Nov 6, 2008 at 11:59 PM, yoma <yo********@gmail.comwrote:
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
Nov 7 '08 #2

P: n/a
On Thu, 06 Nov 2008 23:59:51 -0800, yoma wrote:
import copy

class A:
i = 1

class B:
a = A()
b = B()

x=copy.copy(b)

y=copy.deepcopy(b)

print id(x.a), id(b.a)

print id(y.a), id(y.a)

the result:
14505264 14505264
14505264 14505264

So maybe i have a wrong understand to deep copy and shallow copy or it
is a bug ?
Additionally to Chris' explanation: You will get the same `id()` for
every "copy" of a 1 in CPython. Numbers are immutable so the CPython
implementation caches small integers as an optimization.

Ciao,
Marc 'BlackJack' Rintsch
Nov 7 '08 #3

P: n/a
yoma 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.
Read a little further: This module does not copy types like module,
method, stack trace, stack frame, file, socket, window, array, or any
similar types. It does ``copy'' functions and classes (shallow and
deeply), by returning the original object unchanged
so i try a example:
import copy

class A:
i = 1

class B:
a = A()
b = B()
The only attribute of b itself is .__class__ == B, which as the above
says, is 'copied' by not being copied. So either shallow or deep copies
of b will have .__class__ == the original B with its original instance
of a.

tjr

Nov 7 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.