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)
| 1909 | |
| 1910 | @set_module('numpy') |
| 1911 | def isscalar(element): |
| 1912 | """ |
| 1913 | Returns True if the type of `element` is a scalar type. |
| 1914 | |
| 1915 | Parameters |
| 1916 | ---------- |
| 1917 | element : any |
| 1918 | Input argument, can be of any type and shape. |
| 1919 | |
| 1920 | Returns |
| 1921 | ------- |
| 1922 | val : bool |
| 1923 | True if `element` is a scalar type, False if it is not. |
| 1924 | |
| 1925 | See Also |
| 1926 | -------- |
| 1927 | ndim : Get the number of dimensions of an array |
| 1928 | |
| 1929 | Notes |
| 1930 | ----- |
| 1931 | If you need a stricter way to identify a *numerical* scalar, use |
| 1932 | ``isinstance(x, numbers.Number)``, as that returns ``False`` for most |
| 1933 | non-numerical elements such as strings. |
| 1934 | |
| 1935 | In most cases ``np.ndim(x) == 0`` should be used instead of this function, |
| 1936 | as that will also return true for 0d arrays. This is how numpy overloads |
| 1937 | functions in the style of the ``dx`` arguments to `gradient` and |
| 1938 | the ``bins`` argument to `histogram`. Some key differences: |
| 1939 | |
| 1940 | +------------------------------------+---------------+-------------------+ |
| 1941 | | x |``isscalar(x)``|``np.ndim(x) == 0``| |
| 1942 | +====================================+===============+===================+ |
| 1943 | | PEP 3141 numeric objects | ``True`` | ``True`` | |
| 1944 | | (including builtins) | | | |
| 1945 | +------------------------------------+---------------+-------------------+ |
| 1946 | | builtin string and buffer objects | ``True`` | ``True`` | |
| 1947 | +------------------------------------+---------------+-------------------+ |
| 1948 | | other builtin objects, like | ``False`` | ``True`` | |
| 1949 | | `pathlib.Path`, `Exception`, | | | |
| 1950 | | the result of `re.compile` | | | |
| 1951 | +------------------------------------+---------------+-------------------+ |
| 1952 | | third-party objects like | ``False`` | ``True`` | |
| 1953 | | `matplotlib.figure.Figure` | | | |
| 1954 | +------------------------------------+---------------+-------------------+ |
| 1955 | | zero-dimensional numpy arrays | ``False`` | ``True`` | |
| 1956 | +------------------------------------+---------------+-------------------+ |
| 1957 | | other numpy arrays | ``False`` | ``False`` | |
| 1958 | +------------------------------------+---------------+-------------------+ |
| 1959 | | `list`, `tuple`, and other | ``False`` | ``False`` | |
| 1960 | | sequence objects | | | |
| 1961 | +------------------------------------+---------------+-------------------+ |
| 1962 | |
| 1963 | Examples |
| 1964 | -------- |
| 1965 | >>> import numpy as np |
| 1966 | |
| 1967 | >>> np.isscalar(3.1) |
| 1968 | True |
no outgoing calls
searching dependent graphs…