I have a Python function that I would like to guarantee has been fed a variable, list, tuple, or Numpy array or matrix that consists only of a single number: 2
,(2)
,[2]
, etc. If the function has been passed a tuple, list, array, etc. that consists of more than one value I would like it to raise an error or exception. The function would look something like this:
def f(x):
#check if x is a single variable
if(x.shape != (1)):
#Error or exception and quit
else:
#keep working
However, when I run (for example) f(2)
I am told that 'int' object has no attribute 'shape'
. I am trying to figure out the correct way to do what I describe above in general sense, so that whether x
is a variable, tuple, list, whatever the function can recognize whether it has a single variable or not.