473,473 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

appending to list

Hi,
Iam a newbie to python. So any help on this is
deeply appreciated

If I append an instance of a class to a list will
the list have a copy of the instance or just a
reference to that instance ? If it is only a reference
is there something similar to deep copy in case of
append ?

Thanks,
quent

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

Jul 18 '05 #1
4 2541
On Wed, 1 Oct 2003 08:49:20 -0700 (PDT), sud jag
<qu*********@yahoo.com> wrote:
Hi,
Iam a newbie to python. So any help on this is
deeply appreciated

If I append an instance of a class to a list will
the list have a copy of the instance or just a
reference to that instance ? If it is only a reference
is there something similar to deep copy in case of
append ?

Only a reference. Note though that within the bounds of pure Python
de-referencing is automatic, you can never get hold of the reference
as a Python object. If you want a copy you mst explicitely ask for it
-- check out the copy module, in particular the deepcopy function.
Thanks,
quent


With my best regards,
G. Rodrigues
Jul 18 '05 #2
sud jag wrote:
Hi,
Iam a newbie to python. So any help on this is
deeply appreciated

If I append an instance of a class to a list will
the list have a copy of the instance or just a
reference to that instance ? If it is only a reference
is there something similar to deep copy in case of
append ?


A reference. Python never make copies unless you ASK
for a copy, be it deep or normal (shallow) [note that
deep copying is generally very expensive -- and rarely
needed]. No need for "something similar to deep copy
in case of append": just do

thelist.append(copy.copy(theinstance))

or

thelist.append(copy.deepcopy(theinstance))

depending on what exactly you need (after "import copy",
of course) -- or something more sophisticated if that
should be necessary.

An example of "more sophisticated"...: say that your
need is for all instances appended to 'thelist' to be
"snapshots" from the "outside" viewpoint, BUT it's
all right for them to share their own internals with
each other. In this case, you may take advantage of
the fact that deepcopy takes an optional "memo" dict:
keep the memo (originally empty) around and pass it
to every call to deepcopy. Of course, sophisticated
behavior needs a good grasp of what you're doing, so,
I'm not posting example code lest it tempt you, as a
newbie to python, into facing stuff that's still a bit
beyond your grasp; I'm just planting the seed of the
idea "if and when I do need deep copies, I can take
SOME limited control of the process, in certain cases,
via the ``memo'' dict that deepcopy accepts" -- it will
probably be quite a while before you actually NEED this
information, but, who knows...
Alex

Jul 18 '05 #3
sud jag <qu*********@yahoo.com> writes:
Hi,
Iam a newbie to python. So any help on this is
deeply appreciated

If I append an instance of a class to a list will
the list have a copy of the instance or just a
reference to that instance ? If it is only a reference
is there something similar to deep copy in case of
append ?


Ooh, I get to post this URL:

http://starship.python.net/crew/mwh/objectthink.html

again.

In general, I find that I hardly ever *really* want to use the copy
module Alex pointed you at. Sometimes you do, but you shouldn't reach
for it without due consideration.

Cheers,
mwh

--
The meaning of "brunch" is as yet undefined.
-- Simon Booth, ucam.chat
Jul 18 '05 #4
Michael Hudson <mw*@python.net> writes:
Ooh, I get to post this URL:

http://starship.python.net/crew/mwh/objectthink.html


or, more accurately:

http://starship.python.net/crew/mwh/...jectthink.html

(Soz)

--
I don't remember any dirty green trousers.
-- Ian Jackson, ucam.chat
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bucket79 | last post by:
Hi is there anyway appending to dictionary? list has this feature >>>a = >>>a.append(1) >>>print a but dictionary can't
5
by: rbt | last post by:
I know how to setup an empty list and loop thru something... appending to the list on each loop... how does this work with dicts? I'm looping thru a list of files and I want to put the file's...
0
by: GujuBoy | last post by:
i have an array in C++ (array) and i want to append that to the list can i just say list1.append(array) without actually traversing through the entire array and appending... how can i do...
3
by: MLH | last post by:
I have a query, qryAppend30DayOld260ies that attempts to append records to tblCorrespondence. When run, it can result in any of the following: appending no records, appending 1 record or appending...
7
by: DataSmash | last post by:
I'm confused. Why is it that when I say "while len(list) < 5:", I get 5 items in my list. If I say "while len(list) < 6:", I get 6 items in the list and so on. I would think if I said "less than...
1
by: Ron Adam | last post by:
In my program I have a lot of statements that append elements, but sometimes I don't want to append the element so it requres an if statement to check it, and that requires assigning the returned...
5
by: John Salerno | last post by:
Here's the code: class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY) panel = wx.Panel(self) dbconn = self.connect_db() dbconn.execute("select namefirst,...
10
by: Debajit Adhikary | last post by:
I have two lists: a = b = What I'd like to do is append all of the elements of b at the end of a, so that a looks like: a =
4
by: John [H2O] | last post by:
I have a glob.glob search: searchstring = os.path.join('path'+'EN*') files = glob.glob(searchstring) for f in files: print f ___ This returns some files:
13
by: yueying53 | last post by:
I am new to creating a list of classes. Hence I checked out this website: http://www.daniweb.com/code/snippet390.html. However, I modified the code to check if a class with a particular has been...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.