美文网首页
Python isinstance和type

Python isinstance和type

作者: 斐硕人 | 来源:发表于2021-08-09 18:05 被阅读0次

问题

如何判断变量是不是None或者numpy数组

解决

Python 中有 isinstancetype 两个函数
np.array 的类型为 np.ndarray
nonetype 值为 None

例子

其他

为什么不用==判断None呢?
在LSTM首节点判断时,使用==在首节点之后,也就是变量变为numpy数组之后,会一直报错让使用all()或者any()去判断内部元素,而使用is不会报错。
使用all()或者any()时在首节点为None时又会报错。
原因还没查


参考

  1. Python3 isinstance() 函数 - 菜鸡一枚 - 博客园 (cnblogs.com)
  2. python: how to identify if a variable is an array or a scalar - Stack Overflow
  3. Python判断 NoneType数据类型_fu6543210的博客-CSDN博客
  4. value - python isinstance numpy array - Code Examples (code-examples.net)

相关文章

网友评论

      本文标题:Python isinstance和type

      本文链接:https://www.haomeiwen.com/subject/wsazvltx.html