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

Y

yoma

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!!
 
C

Chris Rebert

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
 
M

Marc 'BlackJack' Rintsch

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
 
T

Terry Reedy

yoma said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top