473,320 Members | 2,092 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,320 software developers and data experts.

strange side effect with lists!?

Hello,

I'm new to Python and playing around. I'm confused by the following
behaviour:
l1 = [1] # define list
l2 = l1 # copy of l1 ?
l2,l1 ([1], [1]) l2.extend(l1) # only l2 should be altered !?
l2,l1

([1, 1], [1, 1]) # but also l1 is altered!

So what is the policy of assignment? When is a copy of an object created?
Where to find dox on this?

thanx,

Wolfgang
Jul 18 '05 #1
5 1439
Wo***************@profactor.at wrote:
l1 = [1] # define list
l2 = l1 # copy of l1 ?
Nope, l2 is l1:
l2 is l1

True

The same happens for any mutable object.

If you want to just do a surface-level copy of l1, you can use l1[:].
But if any of the items in l1 are mutable objects, you probably want to
use copy.deepcopy().
--
Michael Hoffman
Jul 18 '05 #2
Wo***************@profactor.at wrote:
Hello,

I'm new to Python and playing around. I'm confused by the following
behaviour:

l1 = [1] # define list
l2 = l1 # copy of l1 ?
No. Creation of another reference to l1.
you can test identity of objects with the 'is' operator : l2 is l1 True
l2,l1
([1], [1])
l2.extend(l1) # only l2 should be altered !?
No.
l2,l1
([1, 1], [1, 1]) # but also l1 is altered!
Exactly what one would expect !-)
So what is the policy of assignment? When is a copy of an object created?
(please someone correct me if I say any stupidity)

There are two point you need to understand : bindings, and
mutable/immutable types.

With Python, you've got objects, symbols and binding. Unlike languages
like C, a 'variable' (we'd better say 'symbol' or 'name', and you may
also read 'binding') is not the memory address of an object, but a
reference to an object.

The syntax :
l1 = [1] creates a list object, and 'bind' the symbol (or 'name' if you prefer)
'l1' to that list object - which means that l1 holds a reference to the
list object. Now when you're doing l2 = l1 you're binding the symbol l2 to whatever l1 is bound to at this time.

Now there are two kind of objects : mutables and immutables. As
expected, you can modify mutable objects after instanciation, and you
can not modify immutable objects. But you can modify the binding between
a symbol and an immutable object, so when you're doing something like : a = "aaa" and then a = a + "bbb" this do *not* modify the string - what it does is create a new string
made of "aaa" and "bbb" and rebind the symbol 'a' to the newly created
string.

Most Python objects are mutables. Immutable objects are mainly numerics,
strings and tuples.

So what happens with instanciation, modification, bindings, references etc ?

As I said, when you're binding two symbols to the same object (as you
did in your exemple), both symbols reference the same object. Now what
happens when you modify the bound object depends on the "mutability
status" (pardon my poor english) of this object.

Let's have an exemple with an immutable object :

01]>>> a = "aaa"
02]>>> b = a
03]>>> a
04]'aaa'
05]>>> b
06]'aaa'
07]>>> a is b
08]True
09]>>> b = b + "bbb"
10]>>> a
11]'aaa'
12]>>> b
13]'aaabbb'
14]>>> a is b
15]False
16]>>>

Ok, this works as expected : a string being immutable, the statement at
line 09 creates a new string and bind b to this string (or this string
to b if you prefer...). So we now have two distinct string objects

Now with a mutable object :
a = [1]
b = a
a is b True
Ok, now we have to symbols referencing the same mutable object. Since
this is a *mutable* object, modifying it will not create a new object.
So none of the symbols will be rebound :
a.append(2)
a [1, 2] b [1, 2] a is b True
Once again, this works *exactly* as expected - once you understand the
difference between binding and assignement, and the difference between
mutable and immutable objects.

A common pitfall is : a = b = [] This does not create two lists. This create one list and bind both 'a'
and 'b' to this list.

Now back to your problem. You want a copy of the list, not another
reference to the same list. Here the solution is a = [1]
b = a[:]
a is b False a.append(2)
a [1, 2] b [1]


Where to find dox on this?


In the fine manual ?-)
Ok, that's not really obvious from the tutorial. You may want to have a
look here :
http://www.python.org/doc/current/ref/types.html

HTH
Bruno

Jul 18 '05 #3
<Wo***************@profactor.at> wrote:
Hello,

I'm new to Python and playing around. I'm confused by the following
behaviour:
l1 = [1] # define list
l2 = l1 # copy of l1 ?
l2,l1 ([1], [1]) l2.extend(l1) # only l2 should be altered !?
l2,l1

([1, 1], [1, 1]) # but also l1 is altered!

So what is the policy of assignment? When is a copy of an object created?
Where to find dox on this?


Bruno's answer seems very thorough so I'll just try to briefly summarize
the answers:

1. simple assignment (to a bare name, at least), per se, never
implicitly copies objects, but rather it sets a reference to an
object (_another_ reference if the object already had some).

2. a new object is created when you request such creation or perform
operations that require it. Lists are particularly rich in such
operations (see later). Simple assignment to a bare name is not
an operation, per se -- it only affects the name, by making it refer
to whatever object (new, or used;-) is on the righht of the '='.

3. I believe any decent book on Python will cover this in detail.

Now for ways to have a new list object L2 made, with just the same items
and in the same order as another list object L1 ("shallow copy"):

a. import copy; L2 = copy.copy(L1)

This works to shallow-copy _any_ copyable object L1; unfortunately you
do have to import copy first. Module copy also exposes function
deepcopy, for those rare cases in which you wish to recursively also get
copies of all objects to which a "root object" refers (as items, or
attributes; there are some anomalies, e.g., copy.deepcopy(X) is X when X
is a class, or type...).

b. L2 = list(L1)

I find this is most often what I use - it works (making a new list
object) whatever kind of sequence, iterator, or other iterable L1 may
be. It is also what I recommend you use unless you have some very
specific need best met otherwise.

c. various operations such as...:
L2 = L1 * 1
L2 = L1 + []
L2 = L1[:]
i.e. "multiply by one", "concatenate the empty list", or "get a slice of
all items". I'm not sure why, but the latter seems to be very popular,
even though it's neither as concise as L1*1 nor as clear and readable as
list(L1).
Alex
Jul 18 '05 #4
On Wed, 13 Oct 2004 14:19:56 +0200, al*****@yahoo.com (Alex Martelli) wrote:
<Wo***************@profactor.at> wrote:
Hello,

I'm new to Python and playing around. I'm confused by the following
behaviour:
>>> l1 = [1] # define list
>>> l2 = l1 # copy of l1 ?
>>> l2,l1

([1], [1])
>>> l2.extend(l1) # only l2 should be altered !?
>>> l2,l1

([1, 1], [1, 1]) # but also l1 is altered!

So what is the policy of assignment? When is a copy of an object created?
Where to find dox on this?


Bruno's answer seems very thorough so I'll just try to briefly summarize
the answers:

1. simple assignment (to a bare name, at least), per se, never
implicitly copies objects, but rather it sets a reference to an
object (_another_ reference if the object already had some).

2. a new object is created when you request such creation or perform
operations that require it. Lists are particularly rich in such
operations (see later). Simple assignment to a bare name is not
an operation, per se -- it only affects the name, by making it refer
to whatever object (new, or used;-) is on the righht of the '='.

3. I believe any decent book on Python will cover this in detail.

Now for ways to have a new list object L2 made, with just the same items
and in the same order as another list object L1 ("shallow copy"):

a. import copy; L2 = copy.copy(L1)

This works to shallow-copy _any_ copyable object L1; unfortunately you
do have to import copy first. Module copy also exposes function
deepcopy, for those rare cases in which you wish to recursively also get
copies of all objects to which a "root object" refers (as items, or
attributes; there are some anomalies, e.g., copy.deepcopy(X) is X when X
is a class, or type...).

b. L2 = list(L1)

I find this is most often what I use - it works (making a new list
object) whatever kind of sequence, iterator, or other iterable L1 may
be. It is also what I recommend you use unless you have some very
specific need best met otherwise.

c. various operations such as...:
L2 = L1 * 1
L2 = L1 + []
L2 = L1[:]
i.e. "multiply by one", "concatenate the empty list", or "get a slice of
all items". I'm not sure why, but the latter seems to be very popular,
even though it's neither as concise as L1*1 nor as clear and readable as
list(L1).

I got curious:
L = range(5)
L [0, 1, 2, 3, 4]

Make it self-referential: L[2]=L
L [0, 1, [...], 3, 4]
import copy
Lc = copy.copy(L)
Lc [0, 1, [0, 1, [...], 3, 4], 3, 4]
Ldc = copy.deepcopy(L)
Ldc [0, 1, [...], 3, 4]

Interesting that the deep copy made the copy self-referential
(i.e., to the copy itself) like the original's reference to
its (different) self:
id(Ldc) == id(Ldc[2]) True

Unlike the shallow copy: id(Lc) == id(Lc[2]) False
....whose middle reference was merely copied and sill refers to L: id(Lc[2]) == id(L) True

But like the original: id(L) == id(L[2])

True

I'm impressed ;-)

Regards,
Bengt Richter
Jul 18 '05 #5
Bengt Richter wrote:
I got curious:
L = range(5)
L [0, 1, 2, 3, 4]
<various manipulations of a recursive data structure snipped>
I'm impressed ;-)


I find it impressive that recursive data structures involving builtin
types manage to avoid printing out infinite regressions of themselves.
class C: def __init__(self, val):
self.val = val
def __repr__(self):
return "<C %s>" % self.val
l = range(5)
l [0, 1, 2, 3, 4] c = C(l)
c <C [0, 1, 2, 3, 4]> l[2] = c
l [0, 1, <C [...]>, 3, 4] c

<C [0, 1, <C [...]>, 3, 4]>
Jul 18 '05 #6

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

Similar topics

9
by: Kathryn | last post by:
Hiya I have a problem with using some client side and server side scripting together in an ASP. I'm using VBScript. What I'm trying to achieve is this - - Page loads up and some server side...
1
by: Luigi | last post by:
I noted a particular behavior shown by IE. Look at the simple page attached at the bottom of the post. In it, there is a box floated with the float property and another box that jumps it with the...
3
by: Andrew Mayo | last post by:
(note: reason for posting here; browser helper object is written in C++; C++ developers tend to know the intricacies of message handling; this looks like a Windows messaging issue) Microsoft...
9
by: bonono | last post by:
Hi, I initially thought that generator/generator expression is cool(sort of like the lazy evaluation in Haskell) until I notice this side effect. >>>a=(x for x in range(2)) >>>list(a) ...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
14
by: James Wong | last post by:
Hi! everybody, I'm facing a quite strange download problem. I use the following code to download an XML file to client side: With Response ' clear buffer Call .Clear() ' specify the...
20
by: SpreadTooThin | last post by:
I have a list and I need to do a custom sort on it... for example: a = #Although not necessarily in order def cmp(i,j): #to be defined in this thread. a.sort(cmp) print a
6
by: Senthil | last post by:
Hi, Whenever i read a C++ book or a newsgroup posting, i come across the terms like " before the side effects completes" , "destructor with side effects" etc. What is this side effect mean in C++...
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.