473,804 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
+ 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 2555
On Wed, 1 Oct 2003 08:49:20 -0700 (PDT), sud jag
<qu*********@ya hoo.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(thein stance))

or

thelist.append( copy.deepcopy(t heinstance))

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*********@ya hoo.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
3212
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
1892
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 name and its sha hash into a dict on each loop. Many thanks, rbt
0
1120
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 this in BOOST Python please help
3
1934
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 many records. Two of the target fields in tblCorrespondence receiving values in the append operation are and . For any given VehicleJobID value, I want only ONE record in correspondence table to have an value of "01". This query blindly appends...
7
1319
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 5", I would get 4 items. Can anyone explain this? Thanks. R.D. # Start an empty list
1
2304
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 element from a function to a name or calling the funtion twice. e = ET.Element('name') e.append(get_subelement(obj)) # but raises an exception on None
5
1653
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, namelast from master") bb_players =
10
15036
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
1503
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
3227
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 created before. My code works when I create the first class and append it to the list. However, subsequent creations and appending results in this error: "AttributeError: No __call__ method defined." Why is this so? Why does the program from the...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10354
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9177
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4314
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 we have to send another system
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.