Posts

Showing posts with the label python-internals

Why do lists with the same data have different sizes?

Why do lists with the same data have different sizes? Say I create python lists in two ways. lists In the first case I use simple assignment: my_list = print(my_list, '->', my_list.__sizeof__()) my_list = [1] print(my_list, '->', my_list.__sizeof__()) my_list = [1, 1] print(my_list, '->', my_list.__sizeof__()) In the second case I use append() method on list: append() my_list = print(my_list, '->', my_list.__sizeof__()) my_list.append(1) print(my_list, '->', my_list.__sizeof__()) my_list.append(1) print(my_list, '->', my_list.__sizeof__()) But I get unexpected (for me) output: === WITH ASSIGNMENT === (, '->', 40) ([1], '->', 48) ([1, 1], '->', 56) === WITH APPEND === (, '->', 40) ([1], '->', 72) ([1, 1], '->', 72) What happens internally with Python memory management? Why do the 'same' lists have different sizes? I...