I have a function that takes the argument NBins
. I want to make a call to this function with a scalar 50
or an array [0, 10, 20, 30]
. How can I identify within the function, what the length of NBins
is? or said differently, if it is a scalar or a vector?
I tried this:
>>> N=[2,3,5]
>>> P = 5
>>> len(N)
3
>>> len(P)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()
>>>
As you see, I can't apply len
to P
, since it's not an array.... Is there something like isarray
or isscalar
in python?
thanks
>>> import collections.abc
>>> isinstance([0, 10, 20, 30], collections.abc.Sequence) and not isinstance([0, 10, 20, 30], (str, unicode))
True
>>> isinstance(50, collections.abc.Sequence) and not isinstance(50, (str, unicode))
False
note: isinstance
also supports a tuple of classes, check type(x) in (..., ...)
should be avoided and is unnecessary.
You may also wanna check not isinstance(x, (str, unicode))
As noted by @2080 and also here this won't work for numpy
arrays. eg.
>>> import collections.abc
>>> import numpy as np
>>> isinstance((1, 2, 3), collections.abc.Sequence)
True
>>> isinstance(np.array([1, 2, 3]), collections.abc.Sequence)
False
In which case you may try the answer from @jpaddison3:
>>> hasattr(np.array([1, 2, 3]), "__len__")
True
>>> hasattr([1, 2, 3], "__len__")
True
>>> hasattr((1, 2, 3), "__len__")
True
However as noted here, this is not perfect either, and will incorrectly (at least according to me) classify dictionaries as sequences whereas isinstance
with collections.abc.Sequence
classifies correctly:
>>> hasattr({"a": 1}, "__len__")
True
>>> from numpy.distutils.misc_util import is_sequence
>>> is_sequence({"a": 1})
True
>>> isinstance({"a": 1}, collections.abc.Sequence)
False
You could customise your solution to something like this, add more types to isinstance
depending on your needs:
>>> isinstance(np.array([1, 2, 3]), (collections.abc.Sequence, np.ndarray))
True
>>> isinstance([1, 2, 3], (collections.abc.Sequence, np.ndarray))
True