People have wanted to know the size of Python objects for a long time, but before they had to dig into the source code. With the new function sys.getsizeof, you don’t have to.
Here are a list of sizes in bytes of common Python 2.6 datatypes. (containers are empty).
| Type | 32-bit | 64-bit |
| int | 12 | 24 |
| long (1L) | 16 | 32 |
| float | 16 | 24 |
| tuple | 28 | 56 |
| list | 36 | 72 |
| dict | 140 | 280 |
| old class | 48 | 96 |
| old instance | 36 | 72 |
| new class | 452 | 904 |
| new instance | 32 | 64 |
| function | 60 | 120 |
| lambda | 60 | 120 |
| module | 28 | 56 |
| ascii 'a' | 25 | 41 |
| unicode 'a' | 28 | 56 |
| ascii 'ab' | 26 | 42 |
| unicode 'ab' | 30 | 60 |
If you want to see a neat application of getsizeof, take a look at this hack.
int 12
Related posts:
Nice! I wasn’t aware of that. At the least that will help to estimate memory requirements of various programs I write.
Thanks!