tags:

views:

70

answers:

2

in Lists -

I can always check that b=a points to same object and c=a[:] creates another copy.

>>> a = [1,2,3,4,5]
>>> b = a
>>> c = a[:]
>>> a[0] = 10
>>> b
[10, 2, 3, 4, 5]
>>> c
[1, 2, 3, 4, 5]

In Strings -

I cannot make a change to the original immutable string. How do I confirm myself that b=a makes b point to same object, while c = a[:] creates a new copy of the string?

+2  A: 

you can use the is operator.

a = 'aaaaa'
b = 'bbbbb'

print a is b
a = b
print a is b

c = a[:]
print c is a

This works because a is b if and only if id(a) == id(b). In CPython at least, id(foo) is just the memory address at which foo is stored. Hence if foo is bar, then foo and bar are literally the same object. It's interesting to note that

a = 'aaaaa'
b = 'aaaaa'
a is b

is True. This is because python interns (at least most) strings so that it doesn't waste memory storing the same string twice and more importantly can compare strings by comparing fixed length pointers in the C implementation instead of comparing the strings byte by byte.

aaronasterling
`print c is a` gives me `True` (was expecting `False`: `c` is a copy of `a`, stored elsewhere, so `c` and `a` should not be the same objects).
Vaibhav Bajpai
@Vaibhav Bajpai python is interning `a[:]`. Because it is the same string there is no point in storing two copies of it.
aaronasterling
@Vaibhav Bajpai `a = [1, 2, 3]; b = a[:]` creates a shallow copy of `a` because lists are mutable and so it is necessary to keep track of changes that might be made to `b`. strings are not mutable.
aaronasterling
@aaronasterling this completely clears my doubt, thanks!
Vaibhav Bajpai
+1  A: 

It's still possible to have the same string stored in more than one place - see this example.

>>> a="aaaaa"
>>> b=a[:]
>>> b is a
True
>>> b=a[:-1]+a[-1]
>>> b is a
False
>>> b==a
True
>>> 
gnibbler
It's good to know that. I've often wondered about just always checking strings with `is`. Do you know why python does that in this case?
aaronasterling
@aaronasterling, string literals should always be safe. Seems that constructed strings are not tested though. I don't know why it's done that way.
gnibbler
It is definitely *not* safe to assume that identical string literals are stored as the same string. Python makes no guarantees at all in this case. The current CPython implementations happen to optimise string literals in the same compilation unit, and string literals that look like identifiers are interned, but these are both just details of the implementation.
Duncan
@Duncan, indeed. The compiler in CPython matches up occurances of the same literal string and can use the same piece of memory for them (as they are immutable). There is no requirement by the language spec for this to happen though as far as I can tell.
gnibbler