[Numpy-discussion] Fastest way of distinguish a numpy scalar of a python scalar?

Travis Oliphant oliphant at ee.byu.edu
Tue Oct 3 12:47:55 CDT 2006


Tim Hochberg wrote:

>Francesc Altet wrote:
>  
>
>>Hi,
>>
>>I thought that numpy.isscalar was a good way of distinguising a numpy scalar 
>>from a python scalar, but it seems not:
>>
>>  
>>    
>>
>>>>>numpy.isscalar(numpy.string_('3'))
>>>>>        
>>>>>          
>>>>>
>>True
>>  
>>    
>>
>>>>>numpy.isscalar('3')
>>>>>        
>>>>>          
>>>>>
>>True
>>
>>Is there an easy (and fast, if possible) way to check whether an object is a 
>>numpy scalar or a python one?
>>
>>  
>>    
>>
>It looks like isinstance(x, numpy.generic) works, but I didn't test it 
>extensively.
>
>  
>
That should definitely work. All the array scalars are in a hierarchy 
inheriting from numpy.generic.  There are also sub-levels 
(numpy.integer, numpy.inexact, etc...)


-Travis





More information about the Numpy-discussion mailing list