Short Integers in Python

Arnav picture Arnav · Sep 23, 2008 · Viewed 44.6k times · Source

Python allocates integers automatically based on the underlying system architecture. Unfortunately I have a huge dataset which needs to be fully loaded into memory.

So, is there a way to force Python to use only 2 bytes for some integers (equivalent of C++ 'short')?

Answer

Armin Ronacher picture Armin Ronacher · Sep 23, 2008

Nope. But you can use short integers in arrays:

from array import array
a = array("h") # h = signed short, H = unsigned short

As long as the value stays in that array it will be a short integer.