Python: Careful with equal sign
December 8th, 2007 mysurface Posted in python | Hits: 21309 |
To store data, object reference to a variable is very easy in python. For example, to assign an integer 5 to a
a=5
Same thing goes to string
str="hello world"
Assign data to a data structured variable, let say list,
L=[1,2,3,4,5]
Even assign a function to a variable.
import sys
wstderr=sys.stderr.write
wstderr("hello world\n")
Observed that all of them uses equal sign, but CAREFUL! Sometimes, they doesn’t work the same way as you expect them to.
In python, some variables are immutable and some are not. For example, string and tuple are immutable, but list and dictionary accept dynamic changes.
Lets look at string in python
>>> str="my snake bites"
>>> str[4]
'n'
>>> str[4]='f'
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object does not support item assignment
You can’t cannot change value of a particular character in string, they are immutable. The value “my snake bites” is actually store in a new blocks of memory and str will be refers to that particular blocks of memory.
When you do this:
str2=str
The content of str is copied to str2, both of them are independent.
Lets look at list now, you are allow to delete or append data dynamically during runtime.
L=[1,2,3,4,5]
IMPORTANT: The value will be stored into a range of memory blocks, and what if you do this?
L2=L
It make L2 refers to the same memory blocks where L points to. Let see!
>>> L=[1,2,3,4,5]
>>> L2=L
>>> L2.append(6)
>>> print L
[1, 2, 3, 4, 5, 6]
Append values to L2 reflects to L too, they are refers to the same blocks of memory.
OMG! what if I wanna COPY the value from L to L2?
>>> L2=L[0:]
>>> print L2
[1, 2, 3, 4, 5, 6]
>>> L2.append(7)
>>> print L
[1, 2, 3, 4, 5, 6]
>>> print L2
[1, 2, 3, 4, 5, 6, 7]
How about dictionary?
You can use copy() method for that.
>>> D=dict()
>>> D[1]=10
>>> D[2]=20
>>> print D
{1: 10, 2: 20}
>>> D2=D.copy()
>>> print D2
{1: 10, 2: 20}
>>> D2[3]=30
>>> print D2
{1: 10, 2: 20, 3: 30}
>>> print D
{1: 10, 2: 20}
Although all of them uses equal sign, but they act differently, so bare in mind while dive in python.
Live Chat!









December 8th, 2007 at 9:17 am
I suppose you mean a
str[4]does not accept assignment.By replacing
str[4]=fwithstr[4]=’f’, python interpreter will throw an assignment-not-support error, e.g.TypeError: 'str' object does not support item assignment.I guess it’s probably a typo. 8-)
December 8th, 2007 at 12:09 pm
Toydi: Thanks, its a typo.
January 29th, 2008 at 3:52 pm
Hi,
I may not have understood something. You said that when str is a string, the efect of the instruction str2=str is that:
“The content of str is copied to str2, both of them are independent”.
Is it really like this? I was pretty convinced that str2 would store a reference to the same string, so that there would not exist two copies of the string, but one. After all, since strings are immutable there is no danger in that. Wouldn’t it be a waste to build such copies of immutable objects?
Thank you and best regards,
Miguel
January 30th, 2008 at 3:40 pm
Hi Miguel:
If str and str2 are pointing to the same blocks of memory, below is the case will happen:
str=”Hello”
str2=str
str2=”World”
Then str will become “World” as well, but that is not the case.
Maybe at the moment we do str=str2, they are pointing to the same memory, but when i do str2=”World”, str2 will spawn a new memory block. I think this will be the case.
I think you are right at this point, thanks.