strange side effect with lists!?

B

Bruno Desthuilliers

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 :
True

([1], [1])
No.

([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 :
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 doingyou'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 :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 :
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
 
W

Wolfgang.Stoecher

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
 
A

Alex Martelli

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
 
B

Bengt Richter

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:
[0, 1, 2, 3, 4]

Make it self-referential:
[0, 1, [...], 3, 4]
[0, 1, [0, 1, [...], 3, 4], 3, 4]
[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:
True

Unlike the shallow copy:
False
....whose middle reference was merely copied and sill refers to L:
True

But like the original:
True

I'm impressed ;-)

Regards,
Bengt Richter
 
D

Duncan Booth

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,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top