【发布时间】:2012-10-25 16:16:03
【问题描述】:
我知道不建议比较类型,但我有一些代码在 if elif 系列中执行此操作。但是,我对 None 值如何工作感到困惑。
def foo(object)
otype = type(object)
#if otype is None: # this doesn't work
if object is None: # this works fine
print("yep")
elif otype is int:
elif ...
我怎么能和is int等等比较好,但不能和is None比较? types.NoneType 似乎在 Python 3.2 中消失了,所以我不能使用它...
以下
i = 1
print(i)
print(type(i))
print(i is None)
print(type(i) is int)
打印
1
<class 'int'>
False
True
而
i = None
print(i)
print(type(i))
print(i is None)
print(type(i) is None)
打印
None
<class 'NoneType'>
True
False
我猜None 很特别,但是有什么用呢? NoneType 真的存在吗,还是 Python 在骗我?
【问题讨论】:
标签: python python-3.x nonetype