问题描述
在 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.
改进的模块
重新引入了 types.EllipsisType
、types.NoneType
和 types.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 在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!