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:
- A set as 'st'
- A list 'lst'
- A tuple 'tpl'
- A dictionary as 'dct'
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).
1 Answer 1
Python uses a lot of tiny and temporary objects (especially dicts, 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.
Comments
Explore related questions
See similar questions with these tags.
st.__repr__is not the representation of the object, it is the representation of the__repr__method of the object.reprofstwould berepr(st)(orst.__repr__(), which would showset(['orange', 'pear', 'apple', 'banana']). As it happens, that doesn't show you the memory address, so your mistake was actually a useful one!