aph*_*aph 1 python arrays numpy
我正在编写一个传递数组的类。我想引发异常,除非该数组的长度正确。我更愿意使用 python 的“try...except”特性来完成这个,如果传递了一个不正确长度的数组,程序就会退出。如何实现这种行为?
这似乎是为“isinstance”设计的,但我不知道如何要求传递的数组是特定形状的 ndarray。
def func(arr):
if not isinstance(arr, np.ndarray) or arr.shape != (1, 5):
raise ValueError("Passed array is not of the right shape")
...
Run Code Online (Sandbox Code Playgroud)