[Tutor] array and int

Kent Johnson kent37 at tds.net
Fri Jun 26 22:20:44 CEST 2009


On Fri, Jun 26, 2009 at 2:28 PM, Dinesh B
Vadhia<dineshbvadhia at hotmail.com> wrote:
> Say, you create an array['i'] for signed integers (which take a minimum 2
> bytes).  A calculation results in an integer that is larger than the range
> of an 'i'.  Normally, Python will convert an 'i' to a 4-byte 'l' integer.
> But, does the same apply for an array ie. does Python dynamically adjust
> from array['i'] to array['l'']?

No. It's easy enough to try:
In [1]: import array

In [2]: a = array.array('i')

In [3]: a.itemsize
Out[3]: 4

In [5]: a.append(1)

In [6]: a
Out[6]: array('i', [1])

In [7]: x = 2**33

In [8]: x
Out[8]: 8589934592L

In [9]: a[0] = x
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)

C:\Project\Mango\<ipython console> in <module>()

OverflowError: long int too large to convert to int

Kent


More information about the Tutor mailing list