修复 Python 类型错误:Nonetype 类型的对象没有 len() 错误

您所在的位置:网站首页 python里typeerror 修复 Python 类型错误:Nonetype 类型的对象没有 len() 错误

修复 Python 类型错误:Nonetype 类型的对象没有 len() 错误

#修复 Python 类型错误:Nonetype 类型的对象没有 len() 错误 | 来源: 网络整理| 查看: 265

这个紧凑的指南讨论了在 Python 中解决 typeerror: object of type 'nonetype' has no len() 错误。对此有一个简单的解释,为了清楚地理解所有内容,让我们学习一下 Python 中的 NoneType。

在 Python 中修复 typeerror: object of type 'nonetype' has no len() 的错误

当我们为 Python 中的任何数据集分配一个 none 值时,它没有长度,这就是为什么你不能为此使用 len() 函数。以下面的代码为例。

#an object of None type i = None #calling function to check the len len(i)#error

在上面的代码中查找 NoneType 的 i 的长度会出现这个错误。如果你不熟悉你的函数是否可以工作,你可以随时使用内置方法来查找是否有该函数。

对于这个错误,我们可以使用以下代码找到 none 对象的魔法函数。

#an object of None type i = None #calling function to check the len #len(i) #error #built-in / magic functions of None object print(dir(i))

下面将是上述代码的输出,如你所见,没有可用于查找 none 对象长度的 length 函数。

['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3