Reputation: 3391
So I encountered an interesting behaviour in Python 2.7, and I an curious to know why is it happening. I defined the following 4 data structures in python 2.7:
Creating them in python:
st = set(['apple', 'orange', 'apple', 'pear', 'orange', 'banana'])
lst = [1, 2, 3, "4"]
tpl = (1, 2, 3, '4')
dct = {'one': 1, 'two': 2, 'three': 3};
Here is the representation of the 4 objects:
st.__repr__
<method-wrapper '__repr__' of set object at 0x7f234997ba48>
tpl.__repr__
<method-wrapper '__repr__' of tuple object at 0x7f23499667e0>
lst.__repr__
<method-wrapper '__repr__' of list object at 0x7f2349910b48>
dct.__repr__
<method-wrapper '__repr__' of dict object at 0x25af3a0>
As you can see, my dictionary was placed in a completely different memory section (0x25af3a0) compared to others (0x7f23499*****).
My question is, why is the dictionary object got placed to a completely different section of memory?
Python version is 2.7.3 (compiled with GCC 4.7.2 on linux), linux version is 3.18.0-kali3-amd64 #1 SMP Debian 3.18.6-1~kali2 (2015-03-02) x86_64 GNU/Linux).
Upvotes: 1
Views: 155
Reputation: 9944
Python uses a lot of tiny and temporary objects (especially dict
s, for passing arguments, storing attributes, etc). To reduce the overhead of allocating and freeing memory every time such an object is created or destroyed, CPython optimizes by pre-allocating arrays of memory slots for various size of small object.
So when you create a small object, Python doesn't allocate memory the way you would expect. Instead uses its own heuristics to select one of the memory locations it has already set aside. New memory is only allocated when the relevant section of Python's "arena" of pre-allocated small blocks is full.
Upvotes: 1