-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0027_immutable.py
More file actions
47 lines (37 loc) · 1.18 KB
/
0027_immutable.py
File metadata and controls
47 lines (37 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# immutable objects cannot be changed.
# Once it is created changing its value would result to creation of another reference
# of memory address or creation of another object
# built-in types that are immutable: int, float, bool, string, unicode, tuple
print('Immutable')
result = 'Correct'
another_result = result
print(id(result))
print(id(another_result))
result += 'ish'
print(id(result))
print(id(another_result))
# mutable objects can be changed.
# Once it is created, changing its values does not change the memory address or create another object
# built-in types that are mutable: list, dict, set, Bytearray
print('Mutable')
shopping_list = ['milk', 'pasta', 'eggs', 'spam', 'bread', 'rice']
another_list = shopping_list
print(id(shopping_list))
print(id(another_list))
shopping_list += ['cookies'] # merges into the existing list
print(id(shopping_list)) # still have the same memory address
print(shopping_list)
print(another_list)
a = b = c = d = e = f = another_list # same as
# a = another_list
# b = another_list
# c = another_list
# d = another_list
# e = another_list
# f = another_list
print(a)
print('Adding cream')
b.append('cream')
print(c)
print(d)
print(shopping_list)