在下面的示例中,我创建一个自定义类型,然后创建此类型的元素数组,然后使用来针对此类型测试此数组的第一个元素isinstance()
,但出现错误。
import numpy as np
# Here I define a simple type with two fields
my_type_simple = np.dtype([('field_1', int), ('field_2', float)])
# An array using the above type
my_var_simple_1 = np.array([(1, 1), (2, 2)], dtype=my_type_simple)
# For a check, should print [(1, 1.) (2, 2.)]
print(my_var_simple_1)
# For a check, should print True
print(isinstance(my_var_simple_1, np.ndarray))
# The below prints numpy.void - how can I find out that in fact it is 'my_type_simple' ?
print(type(my_var_simple_1[0]))
# The below prints True, at least
print(isinstance(my_var_simple_1[0], type(my_var_simple_1[0])))
# But the below raises an Error: TypeError: isinstance() arg 2 must be a type or tuple of types
print(isinstance(my_var_simple_1[0], my_type_simple))
因此,问题是:如何测试找出了type
的my_var_simple_1[0]
,其实是my_simple_type
?那有可能吗?
尝试一下,看它是否满足您的需求:
my_var_simple_1[0].dtype == my_type_simple
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句