Professional Documents
Culture Documents
Once a string has been created, the contents of the object can never be changed
0x1000
my_var hello
0x1234
abcd
Immutable objects are safe from unintended side-effects
but watch out for immutable collection objects that contain mutable objects
def process(s):
s = s + ‘ world’ Scopes
return s
0x1000
module scope 0x1000
process(my_var)
s hello
hello world
Mutable objects are not safe from unintended side-effects
def process(lst):
lst.append(100)
0x1000
Scopes
my_list = [1, 2, 3]
module scope 0x1000
1
process(my_list) my_list 2
3
100
print(my_list)
[1, 2, 3, 100]
process() scope
lst
Immutable collection objects that contain mutable objects
def process(t):
t[0].append(3)
0x1000
Scopes
my_tuple = ([1,2], ‘a’)
0x1000
module scope
process(my_tuple) my_tuple [1,
[1,2,2]3]
‘a’
print(my_tuple)