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

how are strings immutable in python?

>>h = "aja baja"
>>h += 'e'
h
'aja bajae'
>>>
Jul 6 '08 #1
8 1223
ssecorp wrote:
>>>h = "aja baja"
h += 'e'
h
'aja bajae'
>>>>
The inplace-add operator doesn't mutate the lvalue, it just rebinds it:
>>a = b = "foo"
id(a)
47643036142016
>>a += "bar"
id(a), a
(47643036142064, 'foobar')
>>id(b), b
(47643036142016, 'foo')

Peter
Jul 6 '08 #2
Mel
ssecorp wrote:
>>>h = "aja baja"
h += 'e'
h
'aja bajae'
>>>>
What Peter said, or, to put it another way:

Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>a = b = "aja baja"
a += "e"
print a
aja bajae
>>print b
aja baja

Mutability/immutability makes a difference in programs when different
symbols (or container items) share a value.
Mel.
Jul 6 '08 #3


Peter Otten wrote:
ssecorp wrote:
>>>>h = "aja baja"
h += 'e'
h
'aja bajae'

The inplace-add operator doesn't mutate the lvalue, it just rebinds it:
In Python, neither '=' nor members of the 'op=' family are operators.
They all mark *assignment* or *augmented assignment* statements that
*all* bind objects to targets.

Augmented assignments are, rather obviously, restricted to binding one
object to one target. For 'x op= y', if the object originally bound to
x is mutable, the arithmetic operation part of the augmented assignment
can (should) be implemented by an inplace __i<opname>__ special method
that (normally, but not necessarily) mutates and returns self to be
rebound. Otherwise, the interpreter calls the normal __<opname>__
special method (if it exits) that returns a new object to be bound.

Thus, '+=' is neither an operator nor is the indicated operation
necessarily inplace.

Immutable built-in classes do not have __i<opname>__ methods. So given
that name h is bound to a string,
h += 'e'
has exactly the same effect as
h = h + 'e'
which has exactly the same effect as
h = h.__add__('e')
The same is true for immutable instances of other built-in classes.
Terry Jan Reedy

Jul 6 '08 #4
so if strings were mutable and i did
a = b = "foo"
and then did
a += "bar"
then a and b would be foobar?
Jul 6 '08 #5

"ssecorp" <ci**********@gmail.comwrote in message
news:a0**********************************@z72g2000 hsb.googlegroups.com...
so if strings were mutable and i did
a = b = "foo"
and then did
a += "bar"
then a and b would be foobar?
This can be demonstrated with a list of characters, which *is* mutable:
>>a = b = list('foo')
a += list('bar')
a
['f', 'o', 'o', 'b', 'a', 'r']
>>b
['f', 'o', 'o', 'b', 'a', 'r']

--Mark

Jul 6 '08 #6
so why would you ever want mutability?
seems very counterintuitive and unreliable.
Jul 6 '08 #7
so why would you ever want mutability?
>

seems very counterintuitive and unreliable.
For lists, mutability is fairly natural.

Suppose you have a function f that copies
some items from one list to another. You write it
as

def f(src, dst):
for x in src:
if condition(x):
dst.append(x)

If dst was immutable, an .append method could not
be defined (since it modifies the list itself), so
you would have to write

dst = dst + [x]

However, that would only modify the local variable
dst in the function f - the parameter of the caller
of f would not get modified.

The same holds for many other operations on lists.
You can pass very complex data structures around
(lists of lists of dictionaries etc), and have functions
modify the lists, rather than creating new ones.

For example, to change a two-dimensional matrix
(which is a list of lists), you can write

M[10][11] = 3.14

whereas with immutable lists, you would have to write

M_10_new = M[10][:11] + [3.14] + [M10][12:]
M = M[:10] + [M_10_new] + M[11:]

HTH,
Martin
Jul 7 '08 #8
Lie
On Jul 7, 1:45*am, ssecorp <circularf...@gmail.comwrote:
>h = "aja baja"
# 'aja baja' is created and assigned to the name h
>h += 'e'
# Is the equivalent of:
# h = h + 'e'
#
# In there, a h and 'e' is concatenated and assigned to
# a new string object which is bound to h. The
# original string object which contains
# 'aja baja' is "garbage collected"[1].
>h
# printing h which contains the new string object containing 'aja
bajae'
'aja bajae'
[1] actually python uses reference counting first to choose which
object to delete
Jul 8 '08 #9

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

Similar topics

4
by: Rob Jones | last post by:
Hi all, Anyone know the details about String immutability ? I understand the need to have imutable strings at compile time. However if at runtime I have say 8000 strings and then I make a new...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
9
by: Klaus Neuner | last post by:
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...
4
by: Hans | last post by:
Hi, I want to define a couple of constant strings, like in C: #define mystring "This is my string" or using a const char construction. Is this really not possible in Python? Hans
16
by: InDepth | last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble question to the gurus is: "What have we won with the decision to have string objects immutable? Or did we won?" ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.