On this page
numpy.isscalar
numpy.isscalar(num)[source]-
Returns True if the type of
numis a scalar type.Parameters: -
num : any -
Input argument, can be of any type and shape.
Returns: -
val : bool -
True if
numis a scalar type, False if it is not.
See also
ndim- Get the number of dimensions of an array
Notes
In almost all cases
np.ndim(x) == 0should be used instead of this function, as that will also return true for 0d arrays. This is how numpy overloads functions in the style of thedxarguments togradientand thebinsargument tohistogram. Some key differences:x isscalar(x)np.ndim(x) == 0PEP 3141 numeric objects (including builtins) TrueTruebuiltin string and buffer objects TrueTrueother builtin objects, like pathlib.Path,Exception, the result ofre.compileFalseTruethird-party objects like matplotlib.figure.FigureFalseTruezero-dimensional numpy arrays FalseTrueother numpy arrays FalseFalselist,tuple, and other sequence objectsFalseFalseExamples
>>> np.isscalar(3.1) True >>> np.isscalar(np.array(3.1)) False >>> np.isscalar([3.1]) False >>> np.isscalar(False) True >>> np.isscalar('numpy') TrueNumPy supports PEP 3141 numbers:
>>> from fractions import Fraction >>> np.isscalar(Fraction(5, 17)) True >>> from numbers import Number >>> np.isscalar(Number()) True -
© 2005–2019 NumPy Developers
Licensed under the 3-clause BSD License.
https://docs.scipy.org/doc/numpy-1.17.0/reference/generated/numpy.isscalar.html