Python 3.x 中的 NoneType 在哪里? [英] Where is the NoneType located in Python 3.x?

查看:92
本文介绍了Python 3.x 中的 NoneType 在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 3 中,我想检查 value 是字符串还是 None.

In Python 3, I would like to check whether value is either string or None.

一种方法是

assert type(value) in { str, NoneType }

但是 NoneType 在 Python 中的位置在哪里?

But where is NoneType located in Python?

没有任何导入,使用 NoneType 会产生 NameError: name 'NoneType' is not defined.

Without any import, using NoneType produces NameError: name 'NoneType' is not defined.

推荐答案

types.NoneType 正在 Python 3.10 中重新引入.

types.NoneType is being reintroduced in Python 3.10.

Python 3.10 的新增功能

改进的模块

类型

重新引入了 types.EllipsisTypetypes.NoneTypetypes.NotImplementedType 类,提供了一组易于被类型检查器解释的新类型.(由 Bas van Beek 在 bpo-41810 中贡献.)

Reintroduced the types.EllipsisType, types.NoneType and types.NotImplementedType classes, providing a new set of types readily interpretable by type checkers. (Contributed by Bas van Beek in bpo-41810.)

有关更改的讨论是出于对 types.EllipsisType 的需求,导致 types.NoneType 也成为 为了一致性而添加.

The discussion about the change was motivated by a need for types.EllipsisType, leading to types.NoneType also being added for consistency.

这篇关于Python 3.x 中的 NoneType 在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆