Returns True if the type of `element` is a scalar type. Parameters ---------- element : any Input argument, can be of any type and shape. Returns ------- val : bool True if `element` is a scalar type, False if it is not. See Also -------- n
(element)
| 1854 | |
| 1855 | @set_module('numpy') |
| 1856 | def isscalar(element): |
| 1857 | """ |
| 1858 | Returns True if the type of `element` is a scalar type. |
| 1859 | |
| 1860 | Parameters |
| 1861 | ---------- |
| 1862 | element : any |
| 1863 | Input argument, can be of any type and shape. |
| 1864 | |
| 1865 | Returns |
| 1866 | ------- |
| 1867 | val : bool |
| 1868 | True if `element` is a scalar type, False if it is not. |
| 1869 | |
| 1870 | See Also |
| 1871 | -------- |
| 1872 | ndim : Get the number of dimensions of an array |
| 1873 | |
| 1874 | Notes |
| 1875 | ----- |
| 1876 | If you need a stricter way to identify a *numerical* scalar, use |
| 1877 | ``isinstance(x, numbers.Number)``, as that returns ``False`` for most |
| 1878 | non-numerical elements such as strings. |
| 1879 | |
| 1880 | In most cases ``np.ndim(x) == 0`` should be used instead of this function, |
| 1881 | as that will also return true for 0d arrays. This is how numpy overloads |
| 1882 | functions in the style of the ``dx`` arguments to `gradient` and the ``bins`` |
| 1883 | argument to `histogram`. Some key differences: |
| 1884 | |
| 1885 | +--------------------------------------+---------------+-------------------+ |
| 1886 | | x |``isscalar(x)``|``np.ndim(x) == 0``| |
| 1887 | +======================================+===============+===================+ |
| 1888 | | PEP 3141 numeric objects (including | ``True`` | ``True`` | |
| 1889 | | builtins) | | | |
| 1890 | +--------------------------------------+---------------+-------------------+ |
| 1891 | | builtin string and buffer objects | ``True`` | ``True`` | |
| 1892 | +--------------------------------------+---------------+-------------------+ |
| 1893 | | other builtin objects, like | ``False`` | ``True`` | |
| 1894 | | `pathlib.Path`, `Exception`, | | | |
| 1895 | | the result of `re.compile` | | | |
| 1896 | +--------------------------------------+---------------+-------------------+ |
| 1897 | | third-party objects like | ``False`` | ``True`` | |
| 1898 | | `matplotlib.figure.Figure` | | | |
| 1899 | +--------------------------------------+---------------+-------------------+ |
| 1900 | | zero-dimensional numpy arrays | ``False`` | ``True`` | |
| 1901 | +--------------------------------------+---------------+-------------------+ |
| 1902 | | other numpy arrays | ``False`` | ``False`` | |
| 1903 | +--------------------------------------+---------------+-------------------+ |
| 1904 | | `list`, `tuple`, and other sequence | ``False`` | ``False`` | |
| 1905 | | objects | | | |
| 1906 | +--------------------------------------+---------------+-------------------+ |
| 1907 | |
| 1908 | Examples |
| 1909 | -------- |
| 1910 | >>> np.isscalar(3.1) |
| 1911 | True |
| 1912 | >>> np.isscalar(np.array(3.1)) |
| 1913 | False |
no outgoing calls