[Numpy-discussion] attributes of scalar types - e.g. numpy.int32.itemsize

Travis Oliphant oliphant.travis at ieee.org
Fri Aug 18 18:51:35 CDT 2006


Sebastian Haase wrote:
> On Friday 18 August 2006 15:25, Travis Oliphant wrote:
>   
>> Sebastian Haase wrote:
>>     
>>> On Friday 18 August 2006 11:38, Travis Oliphant wrote:
>>>       
>>>> Sebastian Haase wrote:
>>>>         
>>>>> Hi,
>>>>> array dtype descriptors have an attribute itemsize  that gives the
>>>>> total number of bytes required for an item of that dtype.
>>>>>
>>>>> Scalar types, like  numy.int32, also have that attribute,
>>>>> but it returns "something else" - don't know what:
>>>>>
>>>>>
>>>>> Furthermore there are *lot's*  of more attributes to a scalar dtype,
>>>>> e.g.
>>>>>           
>>>> The scalar types are actual Python types (classes) whereas the dtype
>>>> objects are instances.
>>>>
>>>> The attributes you are seeing of the typeobject are very useful when you
>>>> have an instance of that type.
>>>>
>>>> With numpy.int32.itemsize you are doing the equivalent of
>>>> numpy.dtype.itemsize
>>>>         
>>> but why then do I not get the result 4 ?
>>>       
>> Because it's not a "class" attribute, it's an instance attribute.
>>
>> What does numpy.dtype.itemsize give you?
>>
>>     
> I'm really sorry for being so dumb - but HOW can I get then the number of 
> bytes needed by a given scalar type ?
>
>   
Ah, the real question.  Sorry for not catching it earlier.  I've been in 
"make sure this isn't a bug mode" for a long time.

If you have a scalar type you could create one and then check the itemsize:

int32(0).itemsize

Or you could look at the name and parse out how big it is.

There is also a stored dictionary-like object that returns the number of 
bytes for any data-type recognized:

numpy.nbytes[int32]


-Travis





More information about the Numpy-discussion mailing list