assignment statements: lists vs. strings

K

Klaus Neuner

Hello,

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python slang?

Klaus
string1 = "bla"
string2 = string1
string1 = string1 + "bla"
string1 'blabla'
string2 'bla'

list1 = [1,2]
list2 = list1
list1.append(1)
list1 [1, 2, 1]
list2 [1, 2, 1]
 
D

Duncan Booth

(e-mail address removed) (Klaus Neuner) wrote in
Hello,

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python slang?

Klaus
string1 = "bla"
string2 = string1
string1 = string1 + "bla"
string1 'blabla'
string2 'bla'

list1 = [1,2]
list2 = list1
list1.append(1)
list1 [1, 2, 1]
list2 [1, 2, 1]

You aren't comparing like with like here. You can do exactly the same as
your string example using a list:
list1 = [1,2]
list2 = list1
list1 = list1 + [3,4]
list1 [1, 2, 3, 4]
list2 [1, 2]

The difference is that addition of strings, or lists, or anything else,
creates a new object. The append method of the list type modifies an
existing object. The string types don't have an append method because they
are IMMUTABLE: immutable objects, once created never change their values.
Mutable objects can change their values.

Remember that assignment always creates another reference to an existing
object, it never makes a copy of an object.

Builtin immutable objects include: int, long, float, str, unicode, tuple
Builtin mutable objects include: list, dict
 
T

Terry Reedy

Klaus Neuner said:
Hello,

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings:

You are doing different operations, and hence should not expect the same
result.


Here you add two strings together to get a third, which you bind back to
the first name
string1 'blabla'
string2
'bla'
list1 = [1,2]
list2 = list1
list1.append(1)

Here you mutate a list in place, which you can do because lists are mutable
and which you cannot do with strings. To parallel your string operation,
try

list1 = list1 + [1,2]

before you mutate list1 and you should get [1,2,1,2]

list1 [1, 2, 1]
list2
[1, 2, 1]

Terry J. Reedy
 
D

Dang Griffith

Hello,

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python slang?

Klaus
string1 = "bla"
string2 = string1
string1 = string1 + "bla"
string1 'blabla'
string2 'bla'

list1 = [1,2]
list2 = list1
list1.append(1)
list1 [1, 2, 1]
list2 [1, 2, 1]
Strings are immutable, lists are mutable. String concatenation builds
a new string object. List appending does not build a new list.
Variables/names are references to objects; they are not themselves the
objects.

In the first example, you point both variables/names to the "bla"
string. You then build a new string object "blabla" and point the
string1 variable to that new string. string2 still points to the old
one.

In the second example, you point two variables/names to the [1, 2]
list. You then append an item to that list. The two variables still
point to the same list, which now has an additional element.

The "what is it good for" is the mutable/immutable difference. Think
of it this way--numbers are immutable. If you said:You wouldn't expect the number 3 itself to change. When you add n1 to
3, a new object with the value 4 is created and n1 is reassigned to
point to it. Just as with the string, n2 still points to the "old" 3
object.
--dang
p.s.
This last bit about the numbers is supposed to be conceptual, rather
than a description of the internal implementation.
 
A

Aahz

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python slang?

http://starship.python.net/crew/mwh/hacks/objectthink.html
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code --
not in reams of trivial code that bores the reader to death." --GvR
 
K

Karl =?iso-8859-1?q?Pfl=E4sterer?=

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python slang?

Strings are immutable and lists are mutable objects.

Here you create a new string string1 which is fresh allocated.

If you did `id(string1)' and `id(string2)' before and after the
assignment you would have that after the assignment you had two
different objects.
list1 = [1,2]
list2 = list1
list1.append(1)

Here you modify an existing list in place. But that's not the same as
you did with your string exapmle. With a string you couldn't do that
since a string is immutable.

If you had written list1 = list1 + [1] you would have had a new list
object.
L1 = [1,2]
L2 = L1
id(L1) 10489068
id(L2) 10489068
L1 = L1 + [1]
id(L1) 10488140
id(L2) 10489068
L2 [1, 2]
L1 [1, 2, 1]


KP
 
D

Dan Bishop

Hello,

I would like to understand the reason for the following difference
between dealing with lists and dealing with strings: What is this
difference good for? How it is accounted for in Python slang?

Klaus
string1 = "bla"
string2 = string1
string1 = string1 + "bla"
string1 'blabla'
string2 'bla'

list1 = [1,2]
list2 = list1
list1.append(1)

This is *not* equivalent to the string code. Notice that
>>> list1 = list1 + [1]
>>> list1 [1, 2, 1]
>>> list2
[1, 2]

*does* behave like your string example.
list1 [1, 2, 1]
list2
[1, 2, 1]

Unlike string1, the name "list1" hasn't been rebound since the
assignment "list2 = list1", and therefore they still refer to the same
object.
 
K

Klaus Neuner

Thank you all. I have understood the point now.

Klaus

I would like to ask (Duncan) another question, although it is off
topic: I have seen those small photographs in the header of articles
quite often. How are they produced?
 
D

Duncan Booth

(e-mail address removed) (Klaus Neuner) wrote in
Thank you all. I have understood the point now.

Klaus

I would like to ask (Duncan) another question, although it is off
topic: I have seen those small photographs in the header of articles
quite often. How are they produced?

Do a Google search for X-Face. Some newsreaders recognise the X-Face header
line as containing a small 48x48 pixel monochrome bitmap. You can download
programs to convert from more usual bitmap formats into X-Face format then
you just have to include the header line in all your posts; most
newsreaders will let you add custom header lines fairly easily.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top