[Numpy-discussion] silly isscalar question

Nathaniel Smith njs at pobox.com
Tue May 29 09:42:41 EDT 2012


On Tue, May 29, 2012 at 2:31 PM, Derek Homeier
<derek at astro.physik.uni-goettingen.de> wrote:
> On 29 May 2012, at 15:00, Mark Bakker wrote:
>
>> Why does isscalar('hello') return True?
>>
>> I thought it would check for a number?
>
> No, it checks for something that is of 'scalar type', which probably can be
> translated as 'not equivalent to an array'. Since strings can form numpy arrays,
> I guess the logic behind this is that the string is the atomic block of an array
> of dtype 'S' - for comparison, np.isscalar(['hello']) = False.
> I note the fine distinction between np.isscalar( ('hello') ) and np.isscalar( ('hello'), )...

NB you mean np.isscalar( ('hello',) ), which creates a single-element
tuple. A trailing comma attached to a value in Python normally creates
a tuple, but in a function argument list it is treated as separating
arguments instead, and a trailing empty argument is ignored. The
parentheses need to be around the comma to hide it from from the
argument list parsing rule so that the tuple rule can see it.
(Probably you know this, but for anyone reading the archives later...)

- N



More information about the NumPy-Discussion mailing list