gpt4 book ai didi

python - 为什么我在 Python 'cannot import name NoneType' 中收到错误消息?

转载 作者:太空狗 更新时间:2023-10-29 18:28:04 27 4
gpt4 key购买 nike

我正在尝试将一些代码从 2 转换为 3 以及以下简单脚本

import types
from types import NoneType

结果

ImportError: cannot import name NoneType

如何将上面的 2 转换为 3?

最佳答案

types 模块中不再有 NoneType 引用。您应该直接使用 None 检查身份,即 obj is None。如果您确实需要 NoneType,另一种方法是使用:

NoneType = type(None)

这实际上与之前定义的 types.NoneType 完全相同,在 November 28th, 2007 上被删除之前.

作为旁注,您无需导入模块即可使用 from .. import 语法,因此您可以删除 import types 行如果您不在其他任何地方使用模块引用。

关于python - 为什么我在 Python 'cannot import name NoneType' 中收到错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15844714/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com