473,385 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
3 1565
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oliver Spiesshofer | last post by:
Hi, I am trying to load the LDAP and the MCrypt module under WinXP and PHP Version 4.3.7. The strange thing is that I have several other modules that work just fine: extension=php_bz2.dll...
2
by: logistix at cathoderaymission.net | last post by:
I get the feeling I'm just picking the wrong google search phrase here, because I'm finding nothing. I'm trying to namespace out some embedded python modules. This works: Py_InitModule("game",...
5
by: jeremy.d.brewer | last post by:
Hi, I'm rather new to Python, and I've just written my first Python C module. I was wondering if some more experience Pythonista would look over what I've written and given me some pointers (or...
7
by: Alex | last post by:
Hi all, I've found a module that I think will help me combine fields properly, but I'm unsure how to add or use it with Access 2000. Below is the module I'd like to add: ...
3
by: Johnny M | last post by:
using Access 2003 Pardon the subject line, but I don't have a better word for this strange behavior (or behavior I don't understand!!!) I have a class module named DepreciationFactor. One of...
1
by: alain MONTMORY | last post by:
Hello everybody, I am a newbie to python so I hope I am at the right place to expose my problem..... :-http://www.python.org/doc/2.4.2/ext/pure-embedding.html 5.3 Pure Embedding I download the...
3
by: Mac Campbell | last post by:
For some unknown reason my mdb seemed to drop a module I had named "Utilities". I tried to copy the module back in from a backup copy and got the error message "<<MyProject>> is currently unable to...
3
by: Yitzak | last post by:
Hi I've got a mdb that is recreated monthly/weekly. I've written a bas module with lots of functions in it so that they can be used in queries. What is an easy way to import the bas module...
8
by: Derek Martin | last post by:
I'd like to know if it's possible to code something in Python which would be equivalent to the following C: ---- debug.c ---- #include <stdio.h> bool DEBUG;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.