Python – Object Mutability


my_list = [1, 2, 3]
print(my_list)
print(hex(id(my_list)))
[1, 2, 3]
0x5e020c8
my_list.append(4)
print(my_list)
print(hex(id(my_list)))
[1, 2, 3, 4]
0x5e020c8

my_list_1 = [1, 2, 3]
print(my_list_1)
print(hex(id(my_list_1)))
[1, 2, 3]
0x5ef6dc8
my_list_1 = my_list_1 + [4]
print(my_list_1)
print(hex(id(my_list_1)))
[1, 2, 3, 4]
0x5e02bc8

my_dict = dict(key1='value 1')
print(my_dict)
print(hex(id(my_dict)))
{'key1': 'value 1'}
0x5f20750
my_dict['key1'] = 'modified value 1'
print(my_dict)
print(hex(id(my_dict)))
{'key1': 'modified value 1'}
0x5f20750
my_dict['key2'] = 'value 2'
print(my_dict)
print(hex(id(my_dict)))
{'key1': 'modified value 1', 'key2': 'value 2'}
0x5f20750

a = [1, 2]
b = [3, 4]
t = (a, b)
print(t)
([1, 2], [3, 4])
a.append(3)
b.append(5)
print(t)
([1, 2, 3], [3, 4, 5])

Fermer le menu